0% found this document useful (0 votes)
5 views7 pages

Pract 8

The document outlines a data visualization exercise using the Titanic dataset, which contains 891 passenger records. It includes instructions for importing necessary libraries, loading the dataset, and generating various visualizations, such as survival counts based on gender and class, as well as a histogram of ticket fare distribution. The document emphasizes using the Seaborn library for enhanced visual representation.

Uploaded by

14 Neha Chandele
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Pract 8

The document outlines a data visualization exercise using the Titanic dataset, which contains 891 passenger records. It includes instructions for importing necessary libraries, loading the dataset, and generating various visualizations, such as survival counts based on gender and class, as well as a histogram of ticket fare distribution. The document emphasizes using the Seaborn library for enhanced visual representation.

Uploaded by

14 Neha Chandele
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

pract8

April 16, 2026

Data Visualization I 1. Use the inbuilt dataset ‘titanic’. The dataset contains 891 rows and
contains information about the passengers who boarded the unfortunate Titanic ship. Use the
Seaborn library to see if we can find any patterns in the data. 2. Write a code to check how the
price of the ticket (column name: ‘fare’) for each passenger is distributed by plotting a histogram.
Import Libraries
[9]: # Import required libraries
import pandas as pd # For data handling
import seaborn as sns # For visualization
import [Link] as plt # For plotting graphses

Load Titanic Dataset


[10]: # Load inbuilt Titanic dataset from seaborn
df = sns.load_dataset('titanic')

# Display first 5 rows


print([Link]())

survived pclass sex age sibsp parch fare embarked class \


0 0 3 male 22.0 1 0 7.2500 S Third
1 1 1 female 38.0 1 0 71.2833 C First
2 1 3 female 26.0 0 0 7.9250 S Third
3 1 1 female 35.0 1 0 53.1000 S First
4 0 3 male 35.0 0 0 8.0500 S Third

who adult_male deck embark_town alive alone


0 man True NaN Southampton no False
1 woman False C Cherbourg yes False
2 woman False NaN Southampton yes True
3 woman False C Southampton yes False
4 man True NaN Southampton no True
Basic Information
[11]: # Check dataset structure
print([Link]())

# Statistical summary

1
print([Link]())

<class '[Link]'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 15 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 survived 891 non-null int64
1 pclass 891 non-null int64
2 sex 891 non-null object
3 age 714 non-null float64
4 sibsp 891 non-null int64
5 parch 891 non-null int64
6 fare 891 non-null float64
7 embarked 889 non-null object
8 class 891 non-null category
9 who 891 non-null object
10 adult_male 891 non-null bool
11 deck 203 non-null category
12 embark_town 889 non-null object
13 alive 891 non-null object
14 alone 891 non-null bool
dtypes: bool(2), category(2), float64(2), int64(4), object(5)
memory usage: 80.7+ KB
None
survived pclass age sibsp parch fare
count 891.000000 891.000000 714.000000 891.000000 891.000000 891.000000
mean 0.383838 2.308642 29.699118 0.523008 0.381594 32.204208
std 0.486592 0.836071 14.526497 1.102743 0.806057 49.693429
min 0.000000 1.000000 0.420000 0.000000 0.000000 0.000000
25% 0.000000 2.000000 20.125000 0.000000 0.000000 7.910400
50% 0.000000 3.000000 28.000000 0.000000 0.000000 14.454200
75% 1.000000 3.000000 38.000000 1.000000 0.000000 31.000000
max 1.000000 3.000000 80.000000 8.000000 6.000000 512.329200

[12]: df['fare'].dropna()

[12]: 0 7.2500
1 71.2833
2 7.9250
3 53.1000
4 8.0500

886 13.0000
887 30.0000
888 23.4500
889 30.0000

2
890 7.7500
Name: fare, Length: 891, dtype: float64

Survival Count Plot


[13]: # Count of survived vs not survived
[Link](x='survived', data=df)

[Link]("Survival Count")
[Link]()

Survival Based on Gender


[14]: # Survival count based on gender
[Link](x='survived', hue='sex', data=df)

[Link]("Survival based on Gender")


[Link]()

3
Survival Based on Passenger Class
[15]: # Survival count based on passenger class
[Link](x='survived', hue='class', data=df)

[Link]("Survival based on Class")


[Link]()

4
PART 2: Histogram of Fare
[16]: # Plot histogram of fare
[Link](df['fare'], bins=20)

[Link]("Fare Distribution")
[Link]("Fare")
[Link]("Number of Passengers")

[Link]()

5
Better Visualization using Seaborn
[17]: # Histogram using seaborn
[Link](df['fare'], bins=20, kde=True)

[Link]("Fare Distribution with KDE")


[Link]("Fare")
[Link]("Count")

[Link]()

6
7

You might also like