Reputation: 1202
I have a dataframe:
subtracted Subs hrs
0 281871.120 450878.77 0.00
1 221343.432 229535.34 0.00
2 197454.408 32080.93 0.00
3 41934.000 -9853.07 32080.93
i want to replace positive values (With respect to Subs column) from subtracted column to hrs column.
Expected output:
subtracted Subs hrs
0 281871.120 450878.77 281871.120
1 221343.432 229535.34 221343.432
2 197454.408 32080.93 197454.408
3 41934.000 -9853.07 32080.93
Can anybody suggest me the correct way to do it?
Upvotes: 1
Views: 46
Reputation: 862611
You can replace by mask created by positive values with loc
, where
or numpy.where
:
m = df['Subs'] > 0
df.loc[m, 'hrs'] = df['subtracted']
df['hrs'] = df['subtracted'].where(m, df['hrs'])
df['hrs'] = np.where(m, df['subtracted'], df['hrs'])
print (df)
subtracted Subs hrs
0 281871.120 450878.77 281871.120
1 221343.432 229535.34 221343.432
2 197454.408 32080.93 197454.408
3 41934.000 -9853.07 32080.930
For mask created by negative values is possible use mask
:
m = df['Subs'] < 0
df['hrs'] = np.where(m, df['hrs'], df['subtracted'])
df['hrs'] = df['subtracted'].mask(m, df['hrs'])
Upvotes: 1