Using pandas and numpy, I am trying to process a column in a data framework and want to create a new column with the values ββrelated to it. Therefore, if the value 1 is present in column x, then in the new column it will be equal to a, for value 2 it will be b etc
I can do this for single conditions, i.e.
df['new_col'] = np.where(df['col_1'] == 1, a, n/a)
And I can find an example of several conditions, i.e. if x = 3 or x = 4, the value should be a, but do not do something like if x = 3, the value should be a, and if x = 4, the value c.
I tried just running two lines of code, for example:
df['new_col'] = np.where(df['col_1'] == 1, a, n/a) df['new_col'] = np.where(df['col_1'] == 2, b, n/a)
But obviously, the second line is overwritten. Did I miss something important?
source share