Page 84 - Informatics_Practices_Fliipbook_Class12
P. 84

4.  Consider the DataFrame mentioned in the previous question and write the code snippet for the following queries:
              (i)   Retrieve the columns 'Name' and 'Gender'.
              (ii)  Retrieve rows with 'Age' greater than 25.
              (iii)  Retrieve rows where 'Gender' is Male.
              (iv)  Use the iloc function to select the rows from index 2 to index 4.
              (v)  Set row labels to ['A', 'B', 'C', 'D']. What is the updated DataFrame.
         Ans.  (i)  df[['Name', 'Gender']]
              (ii)  df[df['Age'] > 25]
              (iii)  df[df['Gender'] == 'M']
              (iv)  df.iloc[2:5]
              (v)  df.index = ['A', 'B', 'C', 'D']
           5.  Consider the following Pandas DataFrame df:
                 Name     Age  Gender    Height  Weight
              0  Rohan     25       M       175      70
              1  Jasmine   30       F       160      55
              2  Mohit     28       M       180      80
              3  Anshika   32       M       165      60
              Determine the output of the following statements:
              (i)  print(df.groupby('Gender').count())
              (ii)  print(df['Height'].mean())
              (iii)  print(df['Age'].std())
              (iv)  print(df['Height'].max())
              (v)  print(df.iloc[2]['Name'])
              (vi)  print(df.loc[0:2,'Name':'Height'])
              (vii) print(df['Gender'].value_counts())
              (viii) print(df['Gender'].unique())
              (ix)  print(df.groupby('Gender')['Age'].mean())
              (x)  print(pd.concat([df, df], axis=1))
              (xi)  print(df.rename(columns={'Name': 'Full Name', 'Age': 'Years'}))

         Ans.  (i)  print(df.groupby('Gender').count())
                          Name  Age  Height  Weight
                  Gender
                  F          1    1       1       1
                  M          3    3       3       3
              (ii)  print(df['Height'].mean())
                  170.0
              (iii)  print(df['Age'].std())
                  2.9860788111948193
              (iv)  print(df['Height'].max())
                  180
              (v)  print(df.iloc[2]['Name'])
                  Mohit
              (vi)  print(df.loc[0:2,'Name':'Height'])
                        Name  Age Gender  Height
                  0    Rohan   25      M     175
                  1  Jasmine   30      F     160
                  2    Mohit   28      M     180

          70   Touchpad Informatics Practices-XII
   79   80   81   82   83   84   85   86   87   88   89