0% found this document useful (0 votes)
4 views

AI lab 1

The document contains a series of Python code snippets that analyze Titanic passenger data using a DataFrame. It includes calculations for passenger counts, survival rates, average ages, gender counts, survival rates by gender, passenger distribution by class, and handling missing data. The code demonstrates basic data manipulation and analysis techniques in Python using pandas.

Uploaded by

sarodi1891
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

AI lab 1

The document contains a series of Python code snippets that analyze Titanic passenger data using a DataFrame. It includes calculations for passenger counts, survival rates, average ages, gender counts, survival rates by gender, passenger distribution by class, and handling missing data. The code demonstrates basic data manipulation and analysis techniques in Python using pandas.

Uploaded by

sarodi1891
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

AI lab 1 22k-4323

Q1

print('passenger on titanic',df.PassengerId.count())

Q2

print('\noverall survival rate',df.Survived.mean()*100)

Q3

print('\navg age of passenger',df.Age.mean())


AI lab 1 22k-4323

Q4

# print(df.Sex.count())

mcount=df.Sex.value_counts()['male']

fcount=df.Sex.value_counts()['female']

# OR

mcount=len(df[df['Sex']=='male'])

fcount=len(df[df['Sex']=='female'])

print('\nmale count',mcount)

print('\nfemale count',fcount)
AI lab 1 22k-4323

Q5

mc=df[df['Sex']=='male']

fc=df[df['Sex']=='female']

print('\nsurvived male rate',mc.Survived.mean())

print('\nsurvived female rate',fc.Survived.mean())


AI lab 1 22k-4323

Q6

print('\npassenger in each class',df.value_counts('Pclass'))


AI lab 1 22k-4323
Q7

print('\nsurvial rate each class',df.groupby(['Pclass'])['Survived'].mean())

Q8

print('\navg fare for each class',df.groupby(['Pclass'])['Fare'].mean())


AI lab 1 22k-4323
# Q9

print(len(df[df.SibSp>=1] + df[df.Parch>=1]))

or

family_passengers = df[(df['SibSp'] >= 1) | (df['Parch'] >= 1)]

print(len(family_passengers))
AI lab 1 22k-4323

Q10

By using Dropna() function to remove empty row

Or

By filling them with correct value (eg mean,median or mode) by using flllna() function

You might also like