Skip to main contentSkip to solution

There is a DataFrame 'df' with column 'Gender' and 'Salary'. How to calculate the average of the 'Salary' for each 'Gender' using pivot function?

Solution

✅ Correct Option: 1

pd.pivot_table(df, values='Salary', index='Gender', aggfunc='mean') groups the rows by each unique 'Gender' (the index), takes the 'Salary' column (the values), and applies the mean to each group (the aggfunc). This gives one average salary per gender.


Option 3: pivot_table has no method argument. The aggregation is set with aggfunc.

Option 4: values and index are swapped, so it would try to average 'Gender' grouped by 'Salary'.


We believe the answer could be Option 2 as well, since df.pivot_table(values='Salary', index='Gender', aggfunc='mean') is the same method called on the DataFrame and gives the identical result. However, NTA's final answer key (after all challenges) says Option 1.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question