pandas read csv
A CSV file is a type of file where each line contains a single record, and all the columns are separated from each other via a comma.
- A simple way to store big data sets is to use CSV files (comma separated files).
- The pandas library is one of the open-source Python libraries that provide high-performance, convenient data structures and data analysis tools and techniques for Python programming.
You must install pandas library with command
pip install pandas
- You can read CSV files using the
read_csv()
function.
import pandas as pd csv = pd.read_csv('data.csv') print(csv)
In the above program, the csv_read()
method of pandas library reads the data.csv
file and maps its data into a 2D list.
ID | Programming language | Extension |
---|---|---|
00 | Python | .py |
01 | Java | .java |
02 | C++ | .cpp |
03 | JavaScript | .js |
04 | Svelte | .svelte |
05 | TypeScript | .ts |
In some cases, CSV files do not contain any header. In such cases, the read_csv()
method treats the first row of the CSV file as the dataframe header.
pandas csv
To specify custom headers for your CSV files, you need to pass the list of headers to the names attribute of the read_csv()
method.
import pandas as pd headers = ["Num ID", "Language", "Language Extension"] csv = pd.read_csv('data.csv') print(csv)
In the output below, you can see the custom headers that you passed in the list to the read_csv()
method’s name attribute.
Num ID | Language | Language Extension |
---|---|---|
00 | Python | .py |
01 | Java | .java |
02 | C++ | .cpp |
03 | JavaScript | .js |
04 | Svelte | .svelte |
05 | TypeScript | .ts |