How to Load Pandas DataFrame from CSV?

Load Pandas DataFrame from CSV – read_csv()

To load data into Pandas DataFrame from a CSV file, use pandas.read_csv() function.

In this tutorial, we will learn different scenarios that occur while loading data from CSV to Pandas DataFrame.

Python Pandas - Create DataFrame from CSV

Examples

1. Load CSV data into DataFrame

In this example, we take the following csv file and load it into a DataFrame using pandas.read_csv() method.

data.csv

name,physics,chemistry,algebra
Somu,68,84,78
Kiku,74,56,88
Amol,77,73,82
Lini,78,69,87

Python Program

import pandas as pd
	
# Load dataframe from csv
df = pd.read_csv("data.csv")
print(df)

Output

   name  physics  chemistry  algebra
0  Somu       68         84       78
1  Kiku       74         56       88
2  Amol       77         73       82
3  Lini       78         69       87

The first row in the csv file is taken as column names, and the rest as rows of the dataframe.

2. Load DataFrame from CSV file data with specific delimiter

If you are using a different delimiter to differentiate the items in your data, you can specify that delimiter to read_csv() function using delimiter argument.

Consider the following csv file. In this csv file, the delimiter is a space.

data.csv

name physics chemistry algebra
Somu 68 84 78
Kiku 74 56 88
Amol 77 73 82
Lini 78 69 87

Now we will provide the delimiter as space to read_csv() function.

Python Program

import pandas as pd
	
# Load dataframe from csv
df = pd.read_csv('data.csv', delimiter=' ')
print(df)

Output

   name  physics  chemistry  algebra
0  Somu       68         84       78
1  Kiku       74         56       88
2  Amol       77         73       82
3  Lini       78         69       87

3. Load DataFrame from CSV with no header

If your CSV file does not have a header (column names), you can specify that to read_csv() in two ways.

  1. Pass the argument header=None to pandas.read_csv() function.
  2. Pass the argument names to pandas.read_csv() function, which implicitly makes header=None.

Python Program

import pandas as pd

# Using header argument
df = pd.read_csv('data.csv', header=None)

# Using names argument
df1 = pd.read_csv('data.csv', names=list_of_column_names)

print(df1)

For more options available with read_csv() function, refer https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

Summary

In this Pandas Tutorial, we learned how to load data from CSV file into Pandas DataFrame.

Related Tutorials

Code copied to clipboard successfully 👍