Reputation: 8247
I have following dataframe in pandas
code tank product
1234 1 MS
1234 2 HS
1234 1 HS
1234 1 HS
1235 1 MS
1235 1 HS
1235 1 MS
1245 1 MS
1245 2 HS
I want to find how many tanks have multiple products associated with them, in above dataframe e.g for code 1234 tank 1 has Ms and HS as well There are 2 cases in above dataframe
My Desired Dataframe would be
code tank flag
1234 1 yes
1234 2 no
1235 1 yes
1245 1 no
1245 2 no
How can I do it in pandas?
Upvotes: 2
Views: 753
Reputation: 862611
Use SeriesGroupBy.nunique
for count unique values per groups:
df = df.groupby(['code','tank'])['product'].nunique().reset_index()
print (df)
code tank product
0 1234 1 2
1 1234 2 1
2 1235 1 2
3 1245 1 1
4 1245 2 1
And then extract column with pop
and set values by numpy.where
df['flag'] = np.where(df.pop('product') == 1, 'no', 'yes')
print (df)
code tank flag
0 1234 1 yes
1 1234 2 no
2 1235 1 yes
3 1245 1 no
4 1245 2 no
Upvotes: 4