CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.
CSV files can be read using the Python library called Pandas. This library can be used to read several types of files, including CSV files. We use the library function read_csv(input) to read the CSV file. The URL/path of the CSV file which you want to read is given as the input to the function.
Syntax:
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 :
| Parameter | Use |
|---|---|
| filepath_or_buffer | URL or Dir location of file |
| sep | Stands for seperator, default is ‘, ‘ as in csv(comma seperated values) |
| index_col | Makes passed column as index instead of 0, 1, 2, 3…r
|
| header | Makes passed row/s[int/int list] as header
|
| use_cols | Only uses the passed col[string list] to make data frame |
| squeeze | If true and only one column is passed, returns pandas series |
| skiprows | Skips passed rows in new data frame |
If the given path is invalid ie the file is not present at the given path then the function gives a FileNotFoundError. But if the function successfully reads the file, then it returns an object of type class pandas.core.frame.DataFrame. The returned dataframe(Object) can then be converted to a numpy array by using the function dataframe.to_numpy() this function comes with pandas and returns the numpy array representation of the dataframe. Then onwards we can use arr as a numpy array to perform desired operations.
Example:
# PYthon program to read # CSV file without csv module import pandas as pd #reading a csv file with pandas data_frame = pd.read_csv("pokemon.csv") #give the datatype of a pandas # object print(type(data_frame)) #this function gives us a # brief view of the data. print(data_frame.head) #converting pandas dataframe # to a numpy array. arr = data_frame.to_numpy() print(arr) |
Output:

Recommended Posts:
- Using csv module to read the data in Pandas
- Python | Read csv using pandas.read_csv()
- How to read a CSV file to a Dataframe with custom delimiter in Pandas?
- Python program to print Calendar without calendar or datetime module
- Python | OpenCV program to read and save an Image
- Python program to read character by character from a file
- Python program to read file word by word
- Python program to determine if the given IPv4 Address is reserved using ipaddress module
- Python program to determine if the given IP Address is Public or Private using ipaddress module
- Python - Read blob object in python using wand library
- Working with csv files in Python
- Convert CSV to Excel using Pandas in Python
- Reading CSV files in Python
- Convert JSON to CSV in Python
- Writing CSV files in Python
- Saving Text, JSON, and CSV to a File in Python
- Convert CSV to HTML Table in Python
- Writing data from a Python List to CSV row-wise
- Convert HTML table into CSV file in python
- Load CSV data into List and Dictionary using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

