Reputation: 2735
Is there a simple way to identity groups in a pandas dataframe where a value of a column for a group has changed (or where the group has multiple values for a specific column)?
So for the two companies A and B below, I would like to know which has moved to a different state over time. (Hint, firm A moved from NC to SC).
-------------------
Yr | Company| ST |
-------------------
1 | A | NC |
2 | A | NC |
3 | A | SC |
4 | A | SC |
1 | B | AK |
2 | B | AK |
3 | B | AK |
------------------
Ideally I would like pandas to create a column that shows "Y" when a firm has moved:
---------------------------
Yr | Company| ST |Changed|
---------------------------
1 | A | NC | Y
2 | A | NC | Y
3 | A | SC | Y
4 | A | SC | Y
1 | B | AK | N
2 | B | AK | N
3 | B | AK | N
---------------------------
With numbers it is easy, as the mean number in a group will differ from any of the group values. But with text it is harder ... .
Upvotes: 0
Views: 382
Reputation: 323226
Using transform
with nunique
df.groupby('Company').ST.transform('nunique').ne(1).map({True:'Y',False:'N'})
Out[418]:
0 Y
1 Y
2 Y
3 Y
4 N
5 N
6 N
Name: ST, dtype: object
#df['Change']=df.groupby('Company').ST.transform('nunique').ne(1).map({True:'Y',False:'N'})
Upvotes: 3