Read a CSV File in Python Pandas

Read a CSV File in Python Pandas
How to read a CSV file in python.

This guide will show you how to use the pandas library to read a CSV file in python. Pandas is a widely used open-source tool for data analysis and is extremely powerful.

A CSV file is a great way to store your analysis data. This guide will show you how to read a CSV file from both your local directory, and from an online source. We will also include some extra information on how to make your CSV files available for download with CSV Getter.

Part 1 - Local CSV File

Step 1. Install Pandas

If you have installed python or python3, then this can be done easily with pip or pip3 respectively. The following commands can be used in terminal.

// with pip
pip install pandas

// with pip3
pip3 install pandas

Unsure what pip is? Check out this W3Schools Article.

Step 2. Create a Python Script.

It is best when you do this in a new folder or directory to keep things organised. If you want to do this in terminal, navigate to your working directory with a command like cd <path_to_my_folder>. When you are in the right place, create a python file with the following command.

touch main.py

Alternatively, head to your new folder in you favourite development software, and create the file in the UI.

Step 3. Know where your CSV file is.

In this case, our data is called data.csv and we will put it in the same directory as our python file for simplicity. This way, when it comes to specifying the filepath in the code, it will be more simple.

Step 4. Write some Python Pandas code

Paste the following code block into your main.py file.

import pandas as pd

df = pd.read_csv("data.csv")

print(df.head())
What is this code doing?

import pandas as pd makes the pandas directory available to use in our script, under the pseudonym pd. This means all pandas functions can be used with the prefix pd. .

df = pd.read_csv("data.csv") turns a file into a pandas dataframe called df. The filepath is simply the filename, data.csv. (This is because we placed the file in the same directory as the script). A pandas dataframe is a powerful data object that can be used for analysis.

print(df.head()) will print a sample of the data. df.head() is a shorter version of df with only the first 5 rows of data. print() is the python command for displaying content in the terminal.

Conclusion

Now your CSV file is a pandas dataframe, you can explore the library to perform data analysis in python, and other useful functionality.

Part 2 - Online CSV File

The following URL contains a download for a CSV file.

https://api.csvgetter.com/yj4iuhzWUJnVSE9U44A1

Online URLs like this can be used similar to a filepath with the pandas read_csv() method.

The above example becomes.

import pandas as pd

df = pd.read_csv("https://api.csvgetter.com/yj4iuhzWUJnVSE9U44A1")

print(df.head())

And we are now reading online data with pandas.

You can make a CSV download link like the one above by uploading your CSV here.

Gavin
Gavin