Reading a .csv file with Python and creating a DataFrame

In this chapter I will dive into reading the data in .csv file and creating a DataFrame. A .csv file stands for comma-separated value, so it simply contains a set of values separated by a comma where each line is a new record in plain text.

For this exercise we first make a Python script ‘’. Once opened in an text editor or a code editor such as Visual Studio Code (macOS, Windows, Linux), PyCharm (macOS, Windows, Linux) and many others, we can start writing code.

You can use the ‘restaurants.csv’ file

# Read a .csv file and store the data in a DataFrame
# Import pandas

import pandas as pd

# read the content of 'restaurants.csv' and store it into a DataFrame 'df'
df = pd.read_csv('restaurants.csv')

# Show the first 5 rows of the DataFrame. Default value is 5

# If you want to have these values printed in the console

Depending on your settings, in line 7 you might want to use the path to the file. Which in my case is: /Users/hr/Documents/restaurants.csv

Running our python script results in:

          name   kitchen                   review  rating
0         Sumo  Japanese     Good delicious sushi       8
1  Fried chips  Snackbar  Tasty but a bit too fat       5
2        Oikos     Greek       That was fantastic       8
3   The Garden   British                    So so       6
4      McOnald  American                 horrible       3

Pandas automatically turns the .csv file into a DataFrame. A DataFrame can be seen as a table with data in it.

For more reference:

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.