top of page

Pandas Part 4

How To Read A CSV Files In Python By Using Pandas

  • We can use a function in pandas library called read_csv() to read csv files in python

  • It is mainly used to read a comma-separated values (csv) file into DataFrame object.

  • And also we can do operation on this csv files

  • read_csv() will return an data frame object (A comma-separated values (csv) file is returned as two-dimensional data structure with labeled axes)

Example 1

List of Important Parameters read_csv() takes

  • filepath_or_buffer ---- "Location of the csv file"

  • sep ---- "Delimiter or separator to use (default will be comma , )"

  • header ---- " Which row to use as a column header"

  • skiprows ---- "To skip rows when you are converting the csv files into DF "

  • names ---- "To specify the column labels "

  • na_values ---- "Convert values to Nan values"


  • The first parameter to pass is the location of the csv file

Example 2

  • Try to use \\ or // while passing the location .Otherwise we will get an unicode error while using \

Example 3

  • Gets an error if the specified location is wrong or filename is wrong

Example 4


  • Actually a csv files value will be separated by comma (,) if your files value has any other separator then you can use this parameter (Delimiter or separator to use (default will be comma , )

  • So if we dont use the correct separator our data will not be n a correct format after converting to dataframe object

Example 5

  • Now we will use sep parameter and change to space to get correct output

Example 6


  • In a csv file if the 1 row is a column header then you can use header parameter to specify that 1 row should be consider as header of the dataframe

  • Default header will be 0 means consider 1 row as column label (because row index stars from 0 that's why header=0 means 1 row)

Example 6

Example 6

  • We can change header value to consider a particular row to behave as column label

  • All the above rows will be removed (if header=1 the 0 index row will be removed,if header=2 then 0,1 index row will be removed)

Example 7

  • If header = None means csv file don't have any column label use default column label

  • Don't use any row as column label


  • You can skip rows while you are converting a csv file to dataframe object by using skiprows parameter

Example 8

** skip 1 row

** Here header=0 so 1 row will be given as column label

Example 9

**skiprows=0 means no rows are skipped (skiprows doesnt consider default index)

Example 10

** we can skip multiple rows also


  • List of column names to use while converting to a dataframe object

Example 11

Example 12

  • Whenever we use names if the csv file contains a label the use header=0 to override the column labels


  • This parameter is used to convert values in a csv file to nan (not any number)

  • while using list of values ,the values are replaced from the entire dataframe

Example 13

**in the above example string "p1" is replaced form both name column as well as age column which is wrong we only wanted to change from age column

Example 14

  • while using dictionary ,it changes per specific column

**in the above example string "p1" is replaced from only age column as we have passed dictionary

55 views0 comments

Related Posts

bottom of page