I have a pandas pivot table that looks a little like this:
C             bar       foo
A     B                    
one   A -1.154627 -0.243234
three A -1.327977  0.243234
      B  1.327977 -0.079051
      C -0.832506  1.327977  
two   A  1.327977 -0.128534
      B  0.835120  1.327977
      C  1.327977  0.838040
I'd like to be able to filter out rows where column A has fewer than 2 rows in column B, so that the table above would filter A = one:
C             bar       foo
A     B                    
three A -1.327977  0.243234
      B  1.327977 -0.079051
      C -0.832506  1.327977  
two   A  1.327977 -0.128534
      B  0.835120  1.327977
      C  1.327977  0.838040
How can I do this?
In one line:
In [64]: df[df.groupby(level=0).bar.transform(lambda x: len(x) >= 2).astype('bool')]
Out[64]: 
              bar       foo
two   A  0.944908  0.701687
      B -0.204075  0.713141
      C  0.730844 -0.022302
three A  1.263489 -1.382653
      B  0.124444  0.907667
      C -2.407691 -0.773040
In the upcoming release of pandas (11.1), the new filter method achieves this faster and more intuitively:
In [65]: df.groupby(level=0).filter(lambda x: len(x['bar']) >= 2)
Out[65]: 
              bar       foo
three A  1.263489 -1.382653
      B  0.124444  0.907667
      C -2.407691 -0.773040
two   A  0.944908  0.701687
      B -0.204075  0.713141
      C  0.730844 -0.022302
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With