How to Load Pandas DataFrame from CSV? Examples
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.
Video Tutorial
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)
Explanation
- The program imports the
pandas
library, which is used for data manipulation and analysis. - The
read_csv
function is used to load data from a CSV file named'data.csv'
into a dataframedf
. - By default,
read_csv
assumes the data is comma-separated. - The
print(df)
statement is used to display the contents of the dataframedf
to the console.
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)
Explanation
- The program imports the
pandas
library, which is used for data manipulation and analysis. - The
read_csv
function is used to load data from a CSV file named'data.csv'
into a dataframe. - The
delimiter=' '
argument specifies that the values in the CSV file are separated by spaces, rather than the default comma separator. - The
print(df)
statement is used to display the contents of the dataframedf
to the console.
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.
- Pass the argument header=None to pandas.read_csv() function.
- 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)
Explanation
- The program uses the
pandas
library to read data from a CSV file ('data.csv'). - The
df
dataframe is created by reading the CSV file without headers using theheader=None
argument, which tellspandas
that the CSV does not contain any header row. - The
df1
dataframe is created by reading the CSV file with custom column names passed through thenames
argument.list_of_column_names
is expected to be a predefined list of column names to be used in the dataframe. - The
print(df1)
statement prints the dataframe with the custom column names to the console.
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.