Python Pandas | Titanic Dataset | Summary Statistics

Introduction

In this lab, we will learn how to use Python’s Pandas library to calculate summary statistics for data. We will use the Titanic dataset, which contains data on passengers from the Titanic shipwreck. We will learn how to calcul…


This content originally appeared on DEV Community and was authored by Labby

Introduction

MindMap

In this lab, we will learn how to use Python's Pandas library to calculate summary statistics for data. We will use the Titanic dataset, which contains data on passengers from the Titanic shipwreck. We will learn how to calculate summary statistics, aggregate statistics, and count the number of records by category.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Importing the Dataset

The first step is to import the dataset we will be using.

# Importing pandas library
import pandas as pd

# Reading the dataset
titanic = pd.read_csv("data/titanic.csv")

# Displaying the first five rows of the dataset
titanic.head()

Calculating Summary Statistics

In this step, we will calculate summary statistics for the Titanic dataset.

# Computing the average age of the Titanic passengers
average_age = titanic["Age"].mean()
# Printing the result
print(f"The average age of the Titanic passengers is {average_age}")

# Computing the median age and ticket fare price of the Titanic passengers
median_age_fare = titanic[["Age", "Fare"]].median()
# Printing the result
print(f"The median age and ticket fare price of the Titanic passengers are {median_age_fare}")

Aggregating Statistics Grouped by Category

Next, we will learn how to aggregate statistics grouped by category.

# Computing the average age for male versus female Titanic passengers
average_age_sex = titanic[["Sex", "Age"]].groupby("Sex").mean()
# Printing the result
print(f"The average age for male versus female Titanic passengers is {average_age_sex}")

# Computing the mean ticket fare price for each of the sex and cabin class combinations
mean_fare_sex_class = titanic.groupby(["Sex", "Pclass"])["Fare"].mean()
# Printing the result
print(f"The mean ticket fare price for each of the sex and cabin class combinations is {mean_fare_sex_class}")

Counting Number of Records by Category

Finally, we will count the number of records by category.

# Counting the number of passengers in each of the cabin classes
passengers_per_class = titanic["Pclass"].value_counts()
# Printing the result
print(f"The number of passengers in each of the cabin classes is {passengers_per_class}")

Summary

In this lab, we learned how to calculate summary statistics, aggregate statistics, and count the number of records by category using Python's Pandas library. We used the Titanic dataset to perform these operations. These techniques are fundamental for data analysis and can be applied to any dataset.

🚀 Practice Now: Titanic Passenger Data Analysis with Pandas

Want to Learn More?


This content originally appeared on DEV Community and was authored by Labby


Print Share Comment Cite Upload Translate Updates
APA

Labby | Sciencx (2024-08-06T03:24:25+00:00) Python Pandas | Titanic Dataset | Summary Statistics. Retrieved from https://www.scien.cx/2024/08/06/python-pandas-titanic-dataset-summary-statistics/

MLA
" » Python Pandas | Titanic Dataset | Summary Statistics." Labby | Sciencx - Tuesday August 6, 2024, https://www.scien.cx/2024/08/06/python-pandas-titanic-dataset-summary-statistics/
HARVARD
Labby | Sciencx Tuesday August 6, 2024 » Python Pandas | Titanic Dataset | Summary Statistics., viewed ,<https://www.scien.cx/2024/08/06/python-pandas-titanic-dataset-summary-statistics/>
VANCOUVER
Labby | Sciencx - » Python Pandas | Titanic Dataset | Summary Statistics. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/06/python-pandas-titanic-dataset-summary-statistics/
CHICAGO
" » Python Pandas | Titanic Dataset | Summary Statistics." Labby | Sciencx - Accessed . https://www.scien.cx/2024/08/06/python-pandas-titanic-dataset-summary-statistics/
IEEE
" » Python Pandas | Titanic Dataset | Summary Statistics." Labby | Sciencx [Online]. Available: https://www.scien.cx/2024/08/06/python-pandas-titanic-dataset-summary-statistics/. [Accessed: ]
rf:citation
» Python Pandas | Titanic Dataset | Summary Statistics | Labby | Sciencx | https://www.scien.cx/2024/08/06/python-pandas-titanic-dataset-summary-statistics/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.