假设我有下表:
+----+---+---+
| A | B | C |
+----+---+---+
| 1 | 1 | 3 |
| 2 | 2 | 7 |
| 6 | 3 | 2 |
| -1 | 9 | 0 |
| 2 | 1 | 3 |
| -8 | 8 | 2 |
| 2 | 1 | 9 |
+----+---+---+
如果A列的值为负,则用C列的值更新B列的值。如果不执行任何操作,则
这是所需的输出:
+----+---+---+
| A | B | C |
+----+---+---+
| 1 | 1 | 3 |
| 2 | 2 | 7 |
| 6 | 3 | 2 |
| -1 | 0 | 0 |
| 2 | 1 | 3 |
| -8 | 2 | 2 |
| 2 | 1 | 9 |
+----+---+---+
我一直在尝试下面的代码,但是没有用
#not working
result.loc(result["A"] < 0,result['B'] = result['C'].iloc[0])
答案 0 :(得分:2)
result.B [result.A <0] =结果C
答案 1 :(得分:0)
尝试一下:
df.loc[df['A'] < 0, 'B'] = df['C']