Data analysis in Python: Summarizing CSV data with mean and median

Python Pandas @ Freshers.in

Data analysis is an integral part of various industries, driving decisions and strategies. Python, with its simplicity and powerful libraries, has become the go-to language for data analysts. In this guide, we’ll delve into how you can use Python to read a CSV file and summarize the data, focusing on computing the mean and median of specific columns.

CSV (Comma Separated Values) files are straightforward and widely used for storing tabular data. Python, with its ‘pandas’ library, provides robust tools for handling CSV files. Before diving into the script, ensure you have pandas installed:

The CSV file will contain data with columns named ‘Name’, ‘Age’, and ‘Score’. The names Sachin, Rajesh, Fathima, and Ram will be included in the ‘Name’ column. We’ll calculate the mean and median of the ‘Age’ and ‘Score’ columns.

First, here’s a sample of what the CSV file content might look like:

Name,Age,Score
Sachin,30,85
Rajesh,25,90
Fathima,28,88
Ram,32,92

You can create a file with this content and name it, for instance, sample_data.csv.

Now, here’s the Python script to read this CSV file and calculate the mean and median for the ‘Age’ and ‘Score’ columns:

import pandas as pd

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

# Calculating mean and median for the Age and Score columns
mean_values = data[['Age', 'Score']].mean()
median_values = data[['Age', 'Score']].median()

# Displaying the results
print("Mean Values:\n", mean_values)
print("Median Values:\n", median_values)

This script uses Pandas to read the CSV file and then calculates the mean and median of the ‘Age’ and ‘Score’ columns. Finally, it prints out these values. To test this script, make sure you have the Pandas library installed in your Python environment. If not, you can install it using pip install pandas. Then, run the script in the same directory where your sample_data.csv file is located.

Output

Mean Values:
Age: 28.75
Score: 88.75
Median Values:
Age: 29.0
Score: 89.0
Author: user