Python | Read csv using pandas.read_csv()
To access data from the CSV file, we require a function read_csv() that retrieves data in the form of the Dataframe.
Syntax of read_csv()
Syntax: pd.read_csv(filepath_or_buffer, sep=’ ,’ , header=’infer’, index_col=None, usecols=None, engine=None, skiprows=None, nrows=None)
Parameters:
- filepath_or_buffer: It is the location of the file which is to be retrieved using this function. It accepts any string path or URL of the file.
- sep: It stands for separator, default is ‘, ‘ as in CSV(comma separated values).
- header: It accepts int, a list of int, row numbers to use as the column names, and the start of the data. If no names are passed, i.e., header=None, then, it will display the first column as 0, the second as 1, and so on.
- usecols: It is used to retrieve only selected columns from the CSV file.
- nrows: It means a number of rows to be displayed from the dataset.
- index_col: If None, there are no index numbers displayed along with records.
- skiprows: Skips passed rows in the new data frame.
Read CSV using Pandas read_csv
Before using this function, we must import the Pandas library, we will load the CSV file.
PYTHON3
# Import pandas import pandas as pd # reading csv file pd.read_csv( "example1.csv" ) |
Output:
Example 1: Using sep in read_csv()
In this example, we will manipulate our existing CSV file and then add some special characters to see how the sep parameter works.
Python3
# headbrain1 = "totalbill_tip, sex:smoker, day_time, size # 16.99, 1.01:Female|No, Sun, Dinner, 2 # 10.34, 1.66, Male, No|Sun:Dinner, 3 # 21.01:3.5_Male, No:Sun, Dinner, 3 #23.68, 3.31, Male|No, Sun_Dinner, 2 # 24.59:3.61, Female_No, Sun, Dinner, 4 # 25.29, 4.71|Male, No:Sun, Dinner, 4" # Importing pandas library import pandas as pd # Load the data of csv df = pd.read_csv( 'headbrain1.csv' , sep = '[:, |_]' , engine = 'python' ) # Print the Dataframe df |
Output:
Example 2: Using usecols in read_csv()
Here, we are specifying only 3 columns,i.e.[“tip”, “sex”, “time”] to load and we use the header 0 as its default header.
Python3
df = pd.read_csv( 'example1.csv' , header = 0 , usecols = [ "tip" , "sex" , "time" ]) df |
Output:
Example 3: Using index_col in read_csv()
Here, we use the “sex” index first and then the “tip” index, we can simply reindex the header with index_col parameter.
Python3
df = pd.read_csv( 'example1.csv' , header = 0 , index_col = [ "sex" , "tip" ], usecols = [ "tip" , "sex" , "time" ]) df |
Output:
Example 4: Using nrows in read_csv()
Here, we just display only 5 rows using nrows parameter.
Python3
df = pd.read_csv( 'example1.csv' , header = 0 , index_col = [ "tip" , "sex" ], usecols = [ "tip" , "sex" , "time" ], nrows = 5 ) df |
Output:
Example 5: Using skiprows in read_csv()
The skiprows help to skip some rows in CSV, i.e, here you will observe that the upper row and the last row from the original CSV data have been skipped.
Python3
pd.read_csv( "example1.csv" , skiprows = [ 1 , 12 ]) |
Output:
Please Login to comment...