0

Trying to get this code to show 'OW' in col_a if >0, or 'UW if <0. This works with one, but when I try to do the other the condition doesn't work given some of the data in the cell has been changed to str from an int.

Here is an example DataFrame, note that col_b is dummy data but shows that I need to also reference one column.

df = pd.DataFrame({'col_a':[-1,1,2,3,4,-4], 'col_b':[1,2,3,4,5,6]})

    col_a   col_b
0   -1  1
1   OW  2
2   OW  3
3   OW  4
4   OW  5
5   -4  6

df['col_a'].where(df['col_a'] <= 0, 'OW', inplace = True)
df

    col_a   col_b
0   -1  1
1   OW  2
2   OW  3
3   OW  4
4   OW  5
5   -4  6

df['col_a'].where(df['col_a'] >= 0, 'UW', inplace = True)

TypeError: '>=' not supported between instances of 'str' and 'int'

Thanks!

MMA_Kiwi
  • 15
  • 4

1 Answers1

1

Set both values in one step:

df['col_a'] = np.where(df['col_a'] <= 0, 'OW', 'UW')
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252