Reading Data From Files Using Python

Reading Data From Files Using Python

Learn how to read CSV and JSON files using Python

Chris Achinga
·Oct 5, 2021·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Hi There! Welcome to Data 101.

In this article, I will take you through reading files using Python as you prepare to analyze them.

I will be using Google's Colaboratory tool as my IDE. You don't have to install or set up anything on your laptop/computer to use it, simply go to research.google.com/colaboratory and create a new notebook.

Reading CSV Files

You'll need to upload a data file. To do so, click on the folder icon on the far left of the Notebook:

upload.png

I will be using airlines and airports CSV files

On the new Notebook, import Pandas. Pandas is a Python library that is used for data manipulation.

import pandas as pd

Since we have two files, let's create two variables with the path to the files:

airlines = pd.read_csv('airlines.csv')
airports = pd.read_csv('airports.csv')

The .read_csv from pandas library enables us to read the CSV files. (That simple!).

To view the contents of the files read, we'll use .head() from pandas that will return the first 5 rows of the data loaded.

airlines.head()

airlines.png

airports.head()

airports.png

Reading JSON Files

Get the file airports.json

We'll create the path to the file:

airports_json = pd.read_json('airports.json')

To view the first 5 data objects:

airports_json.head()

airportjson.png

Conclusion

This is probably the first-ever step into data analysis, and we nailed it!

Here is the whole demo file:

 
Share this