dimanche 25 octobre 2015

How to built in if-condition based on group size in a pandas groupby object

this question is probably simple, but I just can't figure out how to do it. I have a dataframe grouped by a column. I want to plot each group, but only if its size is > 2.

Here is my code:
df1=df.groupby('Origin')

import matplotlib.pyplot as plt

for key, group in df1:
        plt.figure()
        group.plot(x='xColumnr', y='yColumn', title=str(key))

I have tried to filter out these groups using df2=df1.filter(lambda group: group.size() > 2) and set df2 in place of df1 in my code, but that gets me the error TypeError: 'numpy.int32' object is not callable. Then I tried

df3=df1.size()
if df3[df3 > 2]:
    plot stuff

which raises the exception 'True and False columns missing'. How can I build in the if condition to plot only groups with a size > 2?

Aucun commentaire:

Enregistrer un commentaire