22 Dec Read CSV in Python Pandas
One of the major benefits of working on Python is the ease of reading and accessing CSV (comma-separated files). For this, Python has the Pandas library. In this lesson, we will learn how to read CSV in Python Pandas using the read_csv() method.
Before moving further, we’ve prepared a video tutorial to read a CSV file in Pandas:
Let’s say we have the following CSV file Students.csv:

Read a CSV
Let us now see an example to read our CSV file Students.csv using the pandas.read_csv() method in Pandas. First, load the CSV file, read the data, and store it in a Pandas DataFrame:
import pandas as pd
# Input CSV file
# Loading CSV in the DataFrame
df = pd.read_csv('Students.csv')
# Display the CSV file records
print("Our DataFrame =\n",df)
Output
Student Rank Marks 0 Amit 1 95 1 Virat 2 90 2 David 3 80 3 Will 4 75 4 Steve 5 65
Display the top n rows of the DataFrame
The head() method will display the first n rows of a DataFrame. The default rows returned are 5.
Let us see an example to display only the top 2 rows. For that, set the parameter of the head() as 2. First, load the CSV file, read the data, and store it in a Pandas DataFrame:
import pandas as pd
# Input CSV file
# Loading CSV in the DataFrame
df = pd.read_csv('Students.csv')
# Display the CSV file records
print("Our DataFrame =\n",df)
# Display the top n rows using the head()
print("Top 2 rows\n",df.head(2))
Output
Student Rank Marks 0 Amit 1 95 1 Virat 2 90
Display the last n rows of a DataFrame
The tail() method will display the last n rows of a DataFrame. The default rows returned are 5.
Let us see an example to display only the bottom 2 rows. For that, set the parameter of the tail() as 2. First, load the CSV file, read the data, and store it in a Pandas DataFrame:
import pandas as pd
# Input CSV file
# Loading CSV in the DataFrame
df = pd.read_csv('Students.csv')
# Display the CSV file records
print("Our DataFrame =\n",df)
# Display the bottom n rows using the tail()
print(df.tail(2))
Output
Student Rank Marks 0 Will 4 75 1 Steve 5 65
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments