Pandas/Python: Set value of one column based on value in another column
one way to do this would be to use indexing with .loc
.
Example
In the absence of an example dataframe, I'll make one up here:
import numpy as npimport pandas as pddf = pd.DataFrame({'c1': list('abcdefg')})df.loc[5, 'c1'] = 'Value'>>> df c10 a1 b2 c3 d4 e5 Value6 g
Assuming you wanted to create a new column c2
, equivalent to c1
except where c1
is Value
, in which case, you would like to assign it to 10:
First, you could create a new column c2
, and set it to equivalent as c1
, using one of the following two lines (they essentially do the same thing):
df = df.assign(c2 = df['c1'])# OR:df['c2'] = df['c1']
Then, find all the indices where c1
is equal to 'Value'
using .loc
, and assign your desired value in c2
at those indices:
df.loc[df['c1'] == 'Value', 'c2'] = 10
And you end up with this:
>>> df c1 c20 a a1 b b2 c c3 d d4 e e5 Value 106 g g
If, as you suggested in your question, you would perhaps sometimes just want to replace the values in the column you already have, rather than create a new column, then just skip the column creation, and do the following:
df['c1'].loc[df['c1'] == 'Value'] = 10# or:df.loc[df['c1'] == 'Value', 'c1'] = 10
Giving you:
>>> df c10 a1 b2 c3 d4 e5 106 g
You can use np.where()
to set values based on a specified condition:
#df c1 c2 c30 4 2 11 8 7 92 1 5 83 3 3 54 3 6 8
Now change values (or set) in column ['c2']
based on your condition.
df['c2'] = np.where(df.c1 == 8,'X', df.c3) c1 c2 c30 4 1 11 8 X 92 1 8 83 3 5 54 3 8 8