Using Python 3.3 and Pandas 0.10
I have a DataFrame that is built from concatenating multiple CSV files. First, I filter out all values in the Name column that conta
Instead of length len
, I think you want to consider the number of unique values of Name in each group. Use nunique()
, and check out this neat recipe for filtering groups.
df[df.groupby('ID').Name.transform(lambda x: x.nunique() == 1).astype('bool')]
If you upgrade to pandas 0.12, you can use the new filter
method on groups, which makes this more succinct and straightforward.
df.groupby('ID').filter(lambda x: x.Name.nunique() == 1)
A general remark: Sometimes, of course, you do want to know the length of the group, but I find that size
is a safer choice than len
, which has been troublesome for me in some cases.