Python | Read csv using pandas.read_csv() - GeeksforGeeks

29 Aug, 2021
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Import Pandas:  
import pandas as pd
  Code #1 : read_csv is an important pandas function to read csv files and do operations on it.  
PYTHON3
# Import pandasimport pandas as pd # reading csv filepd.read_csv("filename.csv")
Opening a CSV file through this is easy. But there are many others thing one can do through this function only to change the returned object completely. For instance, one can read a csv file not only locally, but from a URL through read_csv or one can choose what columns needed to export so that we don’t have to edit the array later.Here is the list of parameters it takes with their Default values. 
pd.read_csv(filepath_or_buffer, sep=’, ‘, delimiter=None, header=’infer’, names=None, index_col=None, usecols=None, squeeze=False, prefix=None, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skipinitialspace=False, skiprows=None, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, skip_blank_lines=True, parse_dates=False, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, iterator=False, chunksize=None, compression=’infer’, thousands=None, decimal=b’.’, lineterminator=None, quotechar='”‘, quoting=0, escapechar=None, comment=None, encoding=None, dialect=None, tupleize_cols=None, error_bad_lines=True, warn_bad_lines=True, skipfooter=0, doublequote=True, delim_whitespace=False, low_memory=True, memory_map=False, float_precision=None)  
Not all of them are much important but remembering these actually save time of performing same functions on own. One can see parameters of any function by pressing shift + tab in jupyter notebook. Useful ones are given below with their usage : 
Makes passed column as index instead of 0, 1, 2, 3...r  
 
Makes passed row/s[int/int list] as header 
 
Refer the link to data set used from here.Code #2 : 
PYTHON3
# importing Pandas libraryimport pandas as pd pd.read_csv(filepath_or_buffer = "pokemon.csv") # makes the passed rows headerpd.read_csv("pokemon.csv", header =[1, 2]) # make the passed column as index instead of 0, 1, 2, 3....pd.read_csv("pokemon.csv", index_col ='Type') # uses passed cols only for data framepd.read_csv("pokemon.csv", usecols =["Type"]) # returns pandas series if there is only one columnpd.read_csv("pokemon.csv", usecols =["Type"],                              squeeze = True)                               # skips the passed rows in new seriespd.read_csv("pokemon.csv",            skiprows = [1, 2, 3, 4])
anikakapoor
sooda367
sagartomar9927
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python String | replace()