Pandas: DataFrame filtering using groupby and a function

前端 未结 2 1851
名媛妹妹
名媛妹妹 2021-01-06 09:44

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

2条回答
  •  一整个雨季
    2021-01-06 09:52

    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.

提交回复
热议问题