first_rows= food_info. head( 3 )
print ( first_rows)
print ( food_info. columns)
print ( food_info. shape)
import pandas as pd
food_info= pd. read_csv( "food_info.csv" )
print ( food_info)
print ( food_info. loc[ [ 2 , 5 , 10 ] ] )
zinc_copper = food_info[ [ "Zinc_(mg)" , "Copper_(mg)" ] ]
print ( zinc_copper)
col_names= food_info. columns. to_list( )
print ( col_names)
gram_colunms= [ ]
for c in col_names:
if c. endswith( "(g)" ) :
gram_colunms. append( c)
print ( gram_colunms)
gram_df= food_info[ gram_colunms]
print ( gram_df. head( 3 ) )
print ( food_info[ "Iron_(mg)" ] )
print ( food_info[ "Iron_(mg)" ] / 1000 )
print ( food_info[ "Iron_(mg)" ] * 2 )
water_energy= food_info[ "Water_(g)" ] * food_info[ "Energ_Kcal" ]
print ( water_energy)
iron_grams= food_info[ "Iron_(mg)" ] / 1000
food_info[ "Iron_(g)" ] = iron_grams
print ( food_info[ "Iron_(g)" ] )
print ( food_info[ "Sodium_(mg)" ] )
food_info. sort_values( "Sodium_(mg)" , inplace= True , ascending= False )
print ( food_info[ "Sodium_(mg)" ] )
import pandas as pd
import numpy as np
titanic= pd. read_csv( "titanic_train.csv" )
titanic. head( )
age= titanic[ "Age" ]
age_is_null= pd. isnull( age)
print ( age_is_null)
age_null_true= age[ age_is_null]
print ( age_null_true)
age_null_count= len ( age_null_true)
print ( age_null_count)
correct_mean_age= titanic[ "Age" ] . mean( )
print ( correct_mean_age)
passenger_classes = [ 1 , 2 , 3 ]
fares_by_class = { }
for this_class in passenger_classes:
pclass_rows = titanic[ titanic[ "Pclass" ] == this_class]
pclass_fares = pclass_rows[ "Fare" ]
fare_for_class = pclass_fares. mean( )
fares_by_class[ this_class] = fare_for_class
print ( fares_by_class)
passenger_survival= titanic. pivot_table( index= "Pclass" , values= "Survived" , aggfunc= np. mean)
print ( passenger_survival)
passenger_age= titanic. pivot_table( index= "Pclass" , values= "Age" , aggfunc= np. mean)
print ( passenger_age)
port_stats= titanic. pivot_table( index= "Embarked" , values= [ "Fare" , "Survived" ] , aggfunc= np. sum )
print ( port_stats)
drop_na_columns= titanic. dropna( axis= 1 )
new_titanic= titanic. dropna( axis= 0 , subset= [ "Age" , "Sex" ] )
print ( new_titanic)
row83= titanic. loc[ 83 , "Age" ]
row1000= titanic. loc[ 766 , "Pclass" ]
print ( row1000)
new_survived= titanic. sort_values( "Age" , ascending= False )
print ( new_survived[ 0 : 10 ] )
titanic_reindexed= new_survived. reset_index( drop= True )
print ( titanic_reindexed)
def hundredth_row ( column) :
hundredth_item= column. iloc[ 99 ]
return hundredth_item
hundredth_row= titanic. apply ( hundredth_row)
print ( hundredth_row)
def not_null_count ( column) :
column_null= pd. isnull( column)
null= column[ column_null]
return len ( null)
column_null_count= titanic. apply ( not_null_count)
print ( column_null_count)
def is_minor ( row) :
if row[ "Age" ] < 18 :
return True
else :
return False
minors = titanic. apply ( is_minor, axis= 1 )
def generate_age_label ( row) :
age = row[ "Age" ]
if pd. isnull( age) :
return "unknown"
elif age < 18 :
return "minor"
else :
return "adult"
age_labels = titanic. apply ( generate_age_label, axis= 1 )
print ( age_labels)
titanic[ 'age_labels' ] = age_labels
age_group_survival = titanic. pivot_table( index= "age_labels" , values= "Survived" )
print ( age_group_survival)