1

I have a set, s, with values :

 s = {'K1', 'K2', 'K3', 'K6'}

And, a dataframe that looks like :

df
   ID Name  Value
0  K1   N1   10.0
1  K2   N2   20.0
2  K3   N3   30.0
3  K4   N4   40.0
4  K5   N5   50.0
5  K6   N6   60.0

I want to add a new column to this dataframe called Flag. The row values of this new column would be True if the ID in that row is in the set else it would False.

So the output should look like :

df
   ID Name  Value  Flag
0  K1   N1   10.0   True  
1  K2   N2   20.0   True  
2  K3   N3   30.0   True
3  K4   N4   40.0   False
4  K5   N5   50.0   False
5  K6   N6   60.0   True

I can do this using a for-loop and marking the rows as True or False. But would like to know what is the cleanest and pandas specific way for doing this succinctly without any loops in the code. You can also use numpy, if necessary.

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
Ram K
  • 1,746
  • 2
  • 14
  • 23

1 Answers1

4

Use isin:

df['Flag'] = df['ID'].isin(s)

Output:

   ID Name  Value   Flag
0  K1   N1   10.0   True
1  K2   N2   20.0   True
2  K3   N3   30.0   True
3  K4   N4   40.0  False
4  K5   N5   50.0  False
5  K6   N6   60.0   True
Scott Boston
  • 147,308
  • 15
  • 139
  • 187