How to load Pandas DataFrame from CSV?

Python

Load Pandas DataFrame from CSV – read_csv()

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

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

Example 1: Load CSV data into a 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 dataframe
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 the column name and the rest as the rows of the dataframe.

Example 2: Load DataFrame from CSV file data with specific delimiter

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

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 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 dataframe
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

Load DataFrame from CSV without header

If your CSV file has no header (column names), you can specify it for read_csv() in two ways.

  1. Pass argument header = None to the pandas.read_csv() function.
  2. Pass argument the names to the pandas.read_csv() function, which implicitly generates 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)

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

Summary

In this Panda Guide we learned how to load data from CSV file into Pandas DataFrame.

Hope this helps!

Source link

Share: