Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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']))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading