Reputation: 5
I have a dataframe with two columns of values by ID. I want to compare col_1 and col_2 to see if any col_1 value by IDs bigger than the whole values of col_2 by ID. How can i achieve this? Dataframe looks like this;
data = {'ID': [0, 1, 1, 2, 2, 2, 3],
'col_1': [5,15,35,50,55,55,75],
'col_2': [10,20,30,40,50,60,70]}
Upvotes: 0
Views: 167
Reputation: 2811
try:
df['result'] = np.where(df['col_1'] > df.groupby('ID')['col_2'].transform('max'), 1, 0)
out:
ID col_1 col_2 result
0 0 5 10 0
1 1 15 20 0
2 1 35 30 1
3 2 50 40 0
4 2 55 50 0
5 2 55 60 0
6 3 75 70 1
Upvotes: 1
Reputation: 75080
IIUC, use:
df['new']=df.col_1.gt(df.col_2).astype(int)
print(df)
ID col_1 col_2 new
0 0 5 10 0
1 1 15 20 0
2 1 35 30 1
3 2 50 40 1
4 2 55 50 1
5 2 55 60 0
6 3 75 70 1
Upvotes: 2