In Pandas, what method is used to calculate the mean, median, and other summary statistics for numerical columns in a DataFrame?

sort_values()
melt()
describe()
pivot()

The correct answer is C. describe().

The describe() method is used to calculate the mean, median, and other summary statistics for numerical columns in a DataFrame. It also calculates the count, minimum, maximum, and standard deviation.

The sort_values() method is used to sort a DataFrame by a specified column or columns.

The melt() method is used to convert a DataFrame with columns of mixed types (e.g., both strings and numbers) into a DataFrame with two columns: a column of categories and a column of values.

The pivot() method is used to pivot a DataFrame, which means to change the orientation of the DataFrame from rows and columns to columns and rows.

Here is an example of how to use the describe() method:

“`
import pandas as pd

df = pd.DataFrame({‘A’: [1, 2, 3], ‘B’: [4, 5, 6], ‘C’: [7, 8, 9]})

df.describe()
“`

This will print the following output:

count mean std dev min max
A 3 2.000000 1.414214 1.000000 3.000000
B 3 4.666667 1.732050 3.000000 6.000000
C 3 7.333333 1.828427 5.000000 9.000000

As you can see, the describe() method has calculated the mean, median, standard deviation, minimum, and maximum for each column in the DataFrame.

Exit mobile version