Introduction to Python Pandas Read CSV & JSON

In This Article, You Will Learn About Python Pandas Read

Python Pandas Read – Before moving ahead, let’s know about Pandas DataFrame

Table of Contents

Pandas Read CSV

One method to store large data sets is using CSV Files (comma-separated CSV files).

CSV files are plain text and are a standard format that anyone, even Pandas can comprehend.

We will be using a CSV file called ‘data.csv’.

Click to Download File

Example – Loading the csv file into a DataFrame.

				
					import pandas as pd

file = pd.read_csv('data.csv')

print(file.to_string()) 

				
			
pandas-read-json

As it is shown above, it returned the data loaded into the file.

Example – Print a sample of csv file.

				
					import pandas as pd

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

print(df) 
				
			
pandas-read-json

As it is shown above, it returned the data loaded into the file.

Pandas Read JSON

Big data sets are usually stored or extracted in JSON.

JSON is a plain text however, it has the structure of an object and is widely known in the programming world, such as Pandas.

We will be using a JSON file called ‘data.json’.

Click to Download File

Example – Loading the JSON file into a DataFrame.

				
					import pandas as pd

file = pd.read_json('data.json')

print(file.to_string()) 

				
			

Note: To print the entire DataFrame, use to_string()

Dictionary as JSON

A JSON object is as equal as Python Dictionary’s formate.

In the event that your JSON code isn’t in the form of a file, but instead inside the form of a Python Dictionary, you can transfer it to DataFrame directly.

Example – Loading a Python Dictionary into a DataFrame.

				
					import pandas as pd

details = {
    "Name": {"A": 10, "B": 20, "C": 30},

    "ID": {'A': 40, 'B': 50, 'C': 60},

    "Roll Number": {'A': 70, 'B': 80, 'C': 90}
}

data = pd.DataFrame(details)

print(data)

				
			

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

1 thought on “Introduction to Python Pandas Read CSV & JSON”

Leave a Comment

Stay in the loop

codingstreets