In the world of data analysis and manipulation, Python stands as a powerful and versatile tool. Two common data formats frequently encountered are CSV (Comma-Separated Values) and JSON (JavaScript Object Notation). In this article, we’ll take you through the essential steps of reading data from CSV and JSON files using Python, providing you with the skills needed to unlock insights from your data.
Reading CSV Files
- Import Pandas
- Specify filename
- read csv file
- print sample output for verification
import pandas as pd
file = 'file.csv'
data = pd.read_csv(file)
print(data.head())
This code uses pandas.read_csv() function to read csv file into a dataframe.
Reading JSON Files
- import JSON Module
- specify filename
- read JSON file
- print sample output for verification
import json
file = 'file.json'
with open(file.json,'r') as jsonfile:
data = json.load(jsonfile)
print(data)
Simple, isn’t it 🙂