Page 85 - Informatics_Practices_Fliipbook_Class12
P. 85

(vii)  print(df['Gender'].value_counts())
                      M    3
                      F    1
                      Name: Gender, dtype: int64
                  (viii) print(df['Gender'].unique())
                      ['M' 'F']
                  (ix)  print(df.groupby('Gender')['Age'].mean())
                      Gender
                      F    30.000000
                      M    28.333333
                      Name: Age, dtype: float64
                  (x)  print(pd.concat([df, df], axis=1))
                            Name  Age Gender  Height  Weight     Name  Age Gender  Height  Weight
                      0    Rohan   25      M     175      70    Rohan   25      M     175      70
                      1  Jasmine   30      F     160      55  Jasmine   30      F     160      55
                      2    Mohit   28      M     180      80    Mohit   28      M     180      80
                      3  Anshika   32      M     165      60  Anshika   32      M     165      60
                  (xi)  print(df.rename(columns={'Name': 'Full Name', 'Age': 'Years'}))
                        Full Name  Years 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
               6.  Consider the DataFrame df mentioned in the previous question and write the code snippet for the following queries:
                  (i)   Retrieve the summary statistics of the DataFrame
                  (ii)  Determine the minimum age.
                  (iii)  Retrieve the number of occurrences for each unique value in the Gender column
                  (iv)  Add a new column BMI to the DataFrame to be calculated as weight/height^2
                  (v)  Concatenate the DataFrame df with the following DataFrame in row-wise manner:

                      df2 = pd.DataFrame({'Name': ['John'], 'Age': [27], 'Gender': ['M'], 'Weight': [75]})
                  (vi)  Write the contents of the DataFrame df to a CSV file named 'details.csv'?
                  (vii)  Group the DataFrame by Gender and calculate the average values for each group?
                  (viii) Drop the Height column from the DataFrame
                  (ix)  Rename the column Age to Years

             Ans.  (i)   summary = df.describe()
                  (ii)  min_age = df['Age'].min()
                  (iii)  gender_counts = df['Gender'].value_counts()
                  (iv)  df['BMI'] = df['Weight'] / (df['Height'] ** 2)
                  (v)  concatenated_row = pd.concat([df, df2], axis=0)
                  (vi)  df.to_csv('data.csv', index=False)
                  (vii)  grouped_data = df.groupby('Gender').mean()
                  (viii) df = df.drop('Height', axis=1)
                  (ix)  df = df.rename(columns={'Age': 'Years'})




                                                                             Data Handling using Pandas DataFrame  71
   80   81   82   83   84   85   86   87   88   89   90