Posts

Showing posts with the label Pandas Groupby

Bar Graph From Dataframe Groupby

Image
Answer : copying data from your link and running df = pd.read_clipboard() then using your code df = df.replace(np.nan,0) df = df.groupby(['home_team'])['arrests'].mean() df.plot.bar() Good one by @piRSuared, and I just buitified his answer :) ## referenced to the answer by @piRSquared df = df.replace(np.nan,0) df = df.groupby(['home_team'])['arrests'].mean() ax = df.plot(kind='bar', figsize=(10,6), color="indigo", fontsize=13); ax.set_alpha(0.8) ax.set_title("My Bar Plot", fontsize=22) ax.set_ylabel("Some Heading on Y-Axis", fontsize=15); plt.show()

Aggregate Unique Values From Multiple Columns With Pandas GroupBy

Answer : Use groupby and agg , and aggregate only unique values by calling Series.unique : df.astype(str).groupby('prop1').agg(lambda x: ','.join(x.unique())) prop2 prop3 prop4 prop1 K20 12,1,66 travis,leo 10.0,4.0 L30 3,54,11,10 bob,john 11.2,10.0 df.astype(str).groupby('prop1', sort=False).agg(lambda x: ','.join(x.unique())) prop2 prop3 prop4 prop1 L30 3,54,11,10 bob,john 11.2,10.0 K20 12,1,66 travis,leo 10.0,4.0 If handling NaNs is important, call fillna in advance: import re df.fillna('').astype(str).groupby('prop1').agg( lambda x: re.sub(',+', ',', ','.join(x.unique())) ) prop2 prop3 prop4 prop1 K20 12,1,66 travis,leo 10.0,4.0 L30 3,54,11,10 bob,john 11.2,10.0