8.1. Exercises

Fruit Salad Grouping and Aggregating

Remember the fruit salad dataframe named fruit_salad? Refer to it for the next two questions.

           name    colour    location    seed   shape  sweetness   water-content  weight
0         apple       red     canada    True   round     True          84         100
1        banana    yellow     mexico   False    long     True          75         120
2    cantaloupe    orange      spain    True   round     True          90        1360
3  dragon-fruit   magenta      china    True   round    False          96         600
4    elderberry    purple    austria   False   round     True          80           5
5           fig    purple     turkey   False    oval    False          78          40
6         guava     green     mexico    True    oval     True          83         450
7   huckleberry      blue     canada    True   round     True          73           5
8          kiwi     brown      china    True   round     True          80          76
9    

Consider this output made from the fruit_salad dataframe:

404 image

Coding questions

Instructions:
Running a coding exercise for the first time, could take a bit of time for everything to load. Be patient, it could take a few minutes.

When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.

Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.

Practice Grouping

Find the mean speed of each column for every Pokemon types using .mean() and .groupby().

Tasks:

  • Make a groupby object on the column type.
  • Find the mean value of each column for each pokemon type using .mean() and save the resulting dataframe as type_means.
  • Obtain the mean speed of each pokemon type from the dataframe type_means by using .loc[].
  • Save it in an object named mean_speed.
  • Display it.
Hint 1
  • Are you grouping by the column named type?
  • Are you using .mean() on the pokemon_type dataframe?
  • Are you naming the mean speed objects correctly?
  • Are you obtaining the mean values using .loc[]?
Fully worked solution:


Practice Aggregating

Let’s practice using .agg()

Tasks:

  • Make a groupby object on the column legendary.
  • Find the maximum and minimum value of each column for each legendary groups using .agg() and save the resulting dataframe as legendary_stats.
  • Display it.
Hint 1
  • Are you grouping by the column named legendary?
  • Are you using .agg() on the legendary_stats dataframe?
  • Are you naming the objects correctly?
Fully worked solution: