When should I use a "groupby" in pandas?
Why Stack Overflow recommends it
Stack Overflow users link it when answering or asking about summing, averaging or plotting a DataFrame column by category with groupby.
Recommended by 1 people across 1 different months, 2017 to 2019.
What it is
A short tutorial from Data School's pandas series explaining the split-apply-combine idea behind groupby. Kevin Markham explains when to reach for groupby and walks through four examples, including aggregation and plotting by group.
What people said
Recommends hqkhan · 2019-01 · Stack Overflow · on “python pandas groupby usage for dataframe”
How about the code below? Using groupby('Country') and then summing the number of medals for countries. import pandas as pd gold = pd.DataFrame({'Country': ['USA', 'india', 'Russia'],'Medals': [15, 3, 9]}) silver = pd.DataFrame({'Country': ['USA', 'india', 'Russia'],'Medals': [29, 2, 16]}) bronze = pd.DataFrame({'Country': ['india', 'USA', 'UK'],'Medals': [4, 28, 27]}) print(pd.concat([gold, silver, bronze]).groupby('Country')['Medals'].sum()) Country Russia 25 UK 27 USA 72 india 9 Name: Medals, dtype: int64 Check this [this video] video out if you want more information. I found it to be really helpful.
Cites Svenno Nito · 2017-09 · Stack Overflow · on “Python: plot panda dataframe with groupby - weird output”
I have a dataframe (of type object, for reasons of inserting lists in certain steps beforehand) with three columns looking like this data stimulus trial 0 2 -2 1 1 2 -2 2 2 2 -2 3 3 2 -2 4 4 2 -2 5 5 2 -2 6 6 1 -2 7 ... 159 1 2.5 16 Currently I'm using seaborn but I'm having difficulties inserting proper legends to my plot. # spi_num is my dataframe sns.swarmplot(x="stimulus", y="data", data=spi_num.astype(np.float), edgecolor="black", linewidth=.9) sns.boxplot(x="stimulus", y="data", data=spi_num.astype(np.float), saturation=1) [LINK] So I have two questions. How do I smoothly integrate a legend with seaborn? And how would I get this plot using pandas plot commands? I thought I would need s
Cites zagzig · 2018-07 · Stack Overflow · on “Summing an entire column with another column in pandas”
I'm trying to sum an entire column by country, but when I use my_df.groupby('COUNTRY').VALUES.mean() It throws a DataError: No numeric types to aggregate And when I use my_df.groupby('COUNTRY').VALUES.sum() It produces really big values that are far from realistic (maybe by adding them as strings together?) Could it be that it interprets the values in the column as strings, or am I using the function the wrong way? I'm trying to accomplish exactly what this guy is doing at 1:45 [this video] i.e the values column contains integers that I want to sum by each country.