masfiq reza
masfiq reza

Reputation: 97

How to select every category of a column in a data frame?

I have a dataframe something like this. enter image description here

I want to calculate the variance and standard deviation of co2_emission for each food_category by grouping and aggregating. And it has to be in this format

print(food_consumption.____(____)['co2_emission'].agg([____]))

This is I have done so far

print(food_consumption. .....(....)['co2_emission'].agg([np.var(food_consumption['co2_emission'], ddof=1),np.sqrt(np.var(food_consumption['co2_emission'], ddof=1))]))

I have to select the each category of the column named food_category. how to do that?

Upvotes: 2

Views: 365

Answers (1)

jezrael
jezrael

Reputation: 863166

Because pandas Series.var and Series.std has default ddof=1 pass them to agg:

print(food_consumption.groupby('food_category')['co2_emission'].agg(['var','std']))

Upvotes: 2

Related Questions