What is the primary purpose of the “agg” method in Pandas when applying multiple aggregation functions to different columns?

Grouping data
Filtering data
Aggregating data
Sorting data

The correct answer is C. Aggregating data.

The agg method in Pandas is used to aggregate data from a DataFrame. It can be used to apply a single aggregation function to all columns, or to apply different aggregation functions to different columns.

For example, the following code uses the agg method to calculate the sum of each column in a DataFrame:

df.agg({'A': 'sum', 'B': 'sum'})

This would return a new DataFrame with two columns: A_sum and B_sum. The A_sum column would contain the sum of all the values in the A column, and the B_sum column would contain the sum of all the values in the B column.

The agg method can also be used to apply different aggregation functions to different columns. For example, the following code uses the agg method to calculate the sum of the A column and the mean of the B column:

df.agg({'A': 'sum', 'B': 'mean'})

This would return a new DataFrame with two columns: A_sum and B_mean. The A_sum column would contain the sum of all the values in the A column, and the B_mean column would contain the mean of all the values in the B column.

The agg method is a powerful tool that can be used to aggregate data from a DataFrame in a variety of ways. It is one of the most commonly used methods in Pandas.

The other options are incorrect because they do not describe the primary purpose of the agg method.

  • Option A: Grouping data. The agg method does not group data. It aggregates data.
  • Option B: Filtering data. The agg method does not filter data. It aggregates data.
  • Option D: Sorting data. The agg method does not sort data. It aggregates data.