0% found this document useful (0 votes)
22 views8 pages

Dspracticalexternak 23 Aug

The document contains Python code snippets for data manipulation and visualization using the Titanic dataset in Google Colab. It demonstrates text transformations, regular expressions, data cleaning, and exploratory data analysis with visualizations such as bar plots and histograms. Key operations include handling missing values, converting categorical data, and plotting survival rates based on gender and passenger class.

Uploaded by

vedvarmishra
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)
22 views8 pages

Dspracticalexternak 23 Aug

The document contains Python code snippets for data manipulation and visualization using the Titanic dataset in Google Colab. It demonstrates text transformations, regular expressions, data cleaning, and exploratory data analysis with visualizations such as bar plots and histograms. Key operations include handling missing values, converting categorical data, and plotting survival rates based on gender and passenger class.

Uploaded by

vedvarmishra
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
You are on page 1/ 8

8/23/24, 10:47 AM Untitled2.

ipynb - Colab

text = "Vedvar Mishra"

upper_text = text.upper()

lower_text = text.lower()

title_text = text.title()

# replacing text

text = "Vedvar Mishra"

new_text= text.replace("mishra","sharma",1)

# regular expression

import re

text="my phone number is 6260971082."

new_text=re.sub(r'\d','#',text)

Start coding or generate with AI.

https://colab.research.google.com/drive/1cX6AODL4N5irOvig7eHabALQMtQhp9ys#scrollTo=nctpcKvWm3ov&printMode=true 1/1
8/23/24, 11:05 AM Untitled1.ipynb - Colab

from google.colab import files


uploaded =files.upload()

Choose Files No file chosen Upload widget is only available when the cell has been executed in the current browser session. Please rerun this cell to
enable.
Saving titanic.csv.csv to titanic.csv.csv

import pandas as pd

titanic_data = pd.read_csv('titanic.csv.csv')

print(titanic_data.head())

PassengerId Survived Pclass Name \


0 846 0 3 Abbing, Mr. Anthony
1 747 0 3 Abbott, Mr. Rossmore Edward
2 280 1 3 Abbott, Mrs. Stanton (Rosa Hunt)
3 309 0 2 Abelson, Mr. Samuel
4 875 1 2 Abelson, Mrs. Samuel (Hannah Wizosky)

Sex Age SibSp Parch Ticket Fare Cabin Embarked


0 male 42.0 0 0 C.A. 5547 7.55 NaN S
1 male 16.0 1 1 C.A. 2673 20.25 NaN S
2 female 35.0 1 1 C.A. 2673 20.25 NaN S
3 male 30.0 1 0 P/PP 3381 24.00 NaN C
4 female 28.0 1 0 P/PP 3381 24.00 NaN C

print(titanic_data.info())

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PassengerId 891 non-null int64
1 Survived 891 non-null int64
2 Pclass 891 non-null int64
3 Name 891 non-null object
4 Sex 891 non-null object
5 Age 714 non-null float64
6 SibSp 891 non-null int64
7 Parch 891 non-null int64
8 Ticket 891 non-null object
9 Fare 891 non-null float64
10 Cabin 204 non-null object
11 Embarked 889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB
None

print(titanic_data.describe())

PassengerId Survived Pclass Age SibSp \


count 891.000000 891.000000 891.000000 714.000000 891.000000
mean 446.000000 0.383838 2.308642 29.699118 0.523008
std 257.353842 0.486592 0.836071 14.526497 1.102743
min 1.000000 0.000000 1.000000 0.420000 0.000000
25% 223.500000 0.000000 2.000000 20.125000 0.000000
50% 446.000000 0.000000 3.000000 28.000000 0.000000
75% 668.500000 1.000000 3.000000 38.000000 1.000000
max 891.000000 1.000000 3.000000 80.000000 8.000000

Parch Fare
count 891.000000 891.000000
mean 0.381594 32.204208
std 0.806057 49.693429
min 0.000000 0.000000
25% 0.000000 7.910400
50% 0.000000 14.454200
75% 0.000000 31.000000
max 6.000000 512.329200

print(titanic_data.isnull().sum())

PassengerId 0
Survived 0
Pclass 0
Name 0
Sex 0
Age 177
SibSp 0
Parch 0
Ticket 0

https://colab.research.google.com/drive/1u2xMNqmQm2rZbExm2iUura81YOqhogp2#scrollTo=Gt8MucUExK8k&printMode=true 1/7
8/23/24, 11:05 AM Untitled1.ipynb - Colab
Fare 0
Cabin 687
Embarked 2
dtype: int64

titanic_data['Age'].fillna(titanic_data['Age'].median(), inplace=True)

titanic_data.drop(columns=['Cabin'], inplace=True)

titanic_data['Sex'] = titanic_data['Sex'].map({'male': 1, 'female': 0})

titanic_data['Pclass'] = titanic_data['Pclass'].astype('category')

import matplotlib.pyplot as plt

import seaborn as sns

plt.figure(figsize=(8,6 ))

<Figure size 800x600 with 0 Axes>

sns.barplot(x='Sex', y='Survived', data=titanic_data)

<Axes: xlabel='Sex', ylabel='Survived'>

plt.title('Survival Rate by Sex')

Text(0.5, 1.0, 'Survival Rate by Sex')

https://colab.research.google.com/drive/1u2xMNqmQm2rZbExm2iUura81YOqhogp2#scrollTo=Gt8MucUExK8k&printMode=true 2/7
8/23/24, 11:05 AM Untitled1.ipynb - Colab
plt.xlabel('Sex (1=Male, 0=Female)')

Text(0.5, 0, 'Sex (1=Male, 0=Female)')

plt.ylabel('Survival Rate')

Text(0, 0.5, 'Survival Rate')

plt.show()

plt.figure(figsize=(8, 6))

<Figure size 800x600 with 0 Axes>

sns.barplot(x='Pclass', y='Survived', data=titanic_data)

https://colab.research.google.com/drive/1u2xMNqmQm2rZbExm2iUura81YOqhogp2#scrollTo=Gt8MucUExK8k&printMode=true 3/7
8/23/24, 11:05 AM Untitled1.ipynb - Colab

<Axes: xlabel='Pclass', ylabel='Survived'>

plt.title('Survival Rate by Passenger Class')

Text(0.5, 1.0, 'Survival Rate by Passenger Class')

plt.xlabel('Passenger Class')

Text(0.5, 0, 'Passenger Class')

plt.ylabel('Survival Rate')

https://colab.research.google.com/drive/1u2xMNqmQm2rZbExm2iUura81YOqhogp2#scrollTo=Gt8MucUExK8k&printMode=true 4/7
8/23/24, 11:05 AM Untitled1.ipynb - Colab

Text(0, 0.5, 'Survival Rate')

plt.show()

Start coding or generate with AI.

plt.figure(figsize=(8, 6))

<Figure size 800x600 with 0 Axes>

titanic_data['Age'].hist(bins=30)

<Axes: >

plt.title('Age Distribution')

https://colab.research.google.com/drive/1u2xMNqmQm2rZbExm2iUura81YOqhogp2#scrollTo=Gt8MucUExK8k&printMode=true 5/7
8/23/24, 11:05 AM Untitled1.ipynb - Colab

Text(0.5, 1.0, 'Age Distribution')

plt.xlabel('Age')

Text(0.5, 0, 'Age')

plt.ylabel('Frequency')

Text(0, 0.5, 'Frequency')

plt.show()

https://colab.research.google.com/drive/1u2xMNqmQm2rZbExm2iUura81YOqhogp2#scrollTo=Gt8MucUExK8k&printMode=true 6/7
8/23/24, 11:05 AM Untitled1.ipynb - Colab
plt.figure(figsize=(8, 6))

<Figure size 800x600 with 0 Axes>

sns.boxplot(x=titanic_data['Age'])

<Axes: xlabel='Age'>

plt.title('Box Plot of Age')

Text(0.5, 1.0, 'Box Plot of Age')

plt.xlabel('Age')

Text(0.5, 0, 'Age')

https://colab.research.google.com/drive/1u2xMNqmQm2rZbExm2iUura81YOqhogp2#scrollTo=Gt8MucUExK8k&printMode=true 7/7

You might also like