pandas set one column equal to another 1

pandas set one column equal to another

In [300]:
df.loc[df['colA'] == 'a', 'colC'] = df['colB']
df['colC'] = df['colC'].fillna(0)
df

Out[300]:
   id colA  colB  colC
0   0    a    13    13
1   1    a    52    52
2   2    b    16     0
3   3    a    34    34
4   4    b   946     0

Here is what the above code is Doing:
1. Create a new column called colC
2. Set the value of colC to the value of colB where colA == ‘a’
3. Fill the remaining values of colC with 0

Similar Posts