关于如何选择DataFrame
中的行并替换这些行中的列中的值,有很多关于SO的问题,但缺少一个用例。要使用示例DataFrame
from this question,
In [1]: df
Out[1]:
apple banana cherry
0 0 3 good
1 1 4 bad
2 2 5 good
如果想要根据另一个列更改单个列,这可行:
df.loc[df.cherry == 'bad', 'apple'] = df.banana * 2
或者这会将值设置为两列:
df.loc[df.cherry == 'bad', ['apple', 'banana'] = np.nan
但这不起作用:
df.loc[df.cherry == 'bad', ['apple', 'banana'] = [df.banana, df.apple]
,因为显然右侧是3x2,而左侧是1x2,因此错误信息
ValueError: Must have equal len keys and value when setting with an ndarray
所以我明白问题是什么,但解决方案是什么?
答案 0 :(得分:2)
您可以尝试IIUC:
df['a'] = df.apple * 3
df['b'] = df.banana * 2
print df
apple banana cherry a b
0 0 3 good 0 6
1 1 4 bad 3 8
2 2 5 good 6 10
df[['a', 'b']] = df.loc[df.cherry == 'bad', ['apple', 'banana']]
print df
apple banana cherry a b
0 0 3 good NaN NaN
1 1 4 bad 1.0 4.0
2 2 5 good NaN NaN
或使用条件values
:
df['a'] = df.apple * 3
df['b'] = df.banana * 2
df.loc[df.cherry == 'bad', ['apple', 'banana']] =
df.loc[df.cherry == 'bad', ['a', 'b']].values
print df
apple banana cherry a b
0 0 3 good 0 6
1 3 8 bad 3 8
2 2 5 good 6 10
原始列的其他选项:
print df[['apple','banana']].shift() * 2
apple banana
0 NaN NaN
1 12.0 6.0
2 2.0 8.0
df.loc[df.cherry == 'bad', ['apple', 'banana']] = df[['apple','banana']].shift() * 2
print df
apple banana cherry
0 6.0 3.0 good
1 12.0 6.0 bad
2 2.0 5.0 good