How to read a CSV file in Python using Pandas• 1 min
CSV files are another industry standard.
They are usually used to store and share table like data.
For example, Microsoft Excel can natively save Worksheets into CSV files.
In Python you can easily work with CSV files.
The Pandas library is often used when working CSV files and Dataframes in general.
Pandas has a really powerful method to manipulate CSV.
Here is an example
import pandas as pd # We import the pandas library df = pd.read_csv("path/to/file.csv", sep=",") # We read the csv file print(df) # We print the dataframe
Here we only specify the path to the file and the separator (sep) but you can add some other parameters listed on the documentation here.
The most useful I've found are the following:
- sep : The character used to separate columns in the csv file. By default it's "," (e.g. Sheet1)
- headers : Usually the row where are the columns names (e.g. 0)
- index_col : The index of the columns we want as index. (e.g. 0)
- skiprows : Whether we should skip any rows (e.g. 1)
- engine : which engine to use. It can be either c or Python, If you are working with large files, "c" might be more adequate (e.g. "c")
Here you are ! You can now read csv files in Python !