Python Track · Part 8 of 9

Working with Data in Python: CSV, JSON and Pandas

Python
Pandas
Data Visualization
Read and write CSV, JSON and XML data in Python, manipulate it with pandas, and visualize it with Matplotlib and Seaborn.
Author

Gujuri Alekha

Published

Oct 20, 2024

8.1 Reading and Writing Data in Different Formats

  • CSV (Comma-Separated Values): CSV is a common file format for storing structured data in a plain text format. Python provides libraries like csv and tools like pandas for reading and writing data in CSV format. You can read data from CSV files and write data to them using these libraries.
  • JSON (JavaScript Object Notation): JSON is a lightweight data interchange format commonly used for configuration files, web APIs, and data storage. Python’s json library allows you to work with JSON data, including reading JSON data from files and converting Python data structures to JSON and vice versa.
  • XML (Extensible Markup Language): XML is a markup language used to store structured data. Python has libraries like xml.etree.ElementTree and lxml for parsing and creating XML documents.

8.2 Data Manipulation with Pandas

  • Pandas: Pandas is a powerful library for data manipulation and analysis. It provides two primary data structures, Series and DataFrame, which make it easy to work with structured data. You can perform various operations, such as filtering, sorting, grouping, merging, and reshaping data using Pandas. It’s especially useful for cleaning and transforming data before analysis.
  • Example: You can read a CSV file into a Pandas DataFrame, filter rows based on conditions, compute summary statistics, and perform various data manipulation tasks.
Show code
import pandas as pd

# Read data from a CSV file
data = pd.read_csv('data.csv')

# Filter data
filtered_data = data[data['column_name'] > 10]

# Compute summary statistics
mean_value = data['column_name'].mean()

# Group data
grouped_data = data.groupby('category_column')

8.3 Data Visualization with Matplotlib or Seaborn

  • Matplotlib: Matplotlib is a popular library for creating static, animated, and interactive visualizations in Python. It provides a wide range of options for customizing plots, charts, and graphs.
  • Seaborn: Seaborn is a data visualization library that is built on top of Matplotlib and provides a high-level interface for creating attractive and informative statistical graphics. It simplifies the process of creating complex plots.
  • Example: You can use Matplotlib or Seaborn to create various types of charts, including line plots, bar charts, scatter plots, histograms, and more.
Show code
import matplotlib.pyplot as plt
import seaborn as sns

# Create a line plot
plt.plot(x_values, y_values)
plt.xlabel('X-axis label')
plt.ylabel('Y-axis label')
plt.title('Line Plot')
plt.show()

# Create a bar chart with Seaborn
sns.barplot(x='category', y='value', data=data)
plt.xlabel('Category')
plt.ylabel('Value')
plt.title('Bar Chart')
plt.show()

Citation

BibTeX citation:
@online{alekha2024,
  author = {Alekha, Gujuri},
  title = {Working with {Data} in {Python:} {CSV,} {JSON} and {Pandas}},
  date = {2024-10-20},
  url = {https://gujurialekha.github.io/blog/python/08-python-working-with-data.html},
  langid = {en}
}