python Pandas : change one column conditional on another

标签 python pandas

当侧栏有卖出时,如何反转数量栏中的符号?不应更改所有其他值。下面的根本就不行,没有效果。

df[df['side'] == 'Sell']['quantity'] = df[df['side'] == 'Sell']['quantity'] * -1

最佳答案

引用Pandas indexing documentation ,并且在设置值时切勿根据您的示例使用链索引。

df.loc[df.side == 'Sell', 'quantity'] *= -1

关于 python Pandas : change one column conditional on another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39415286/

相关文章:

python - Office 365 REST API (Python) 将电子邮件标记为已读

javascript - 第一次请求加载json数据并显示在首页

python - 更改 networkx 多图中的边属性

python - Libvirt:更改运行域的网络接口(interface)

pandas - 对一些列进行分组并计算移动的一列和另一列之间的最大差异

python - Excel 等效于在 Python pandas 中使用的数组

python - 按日期对列进行排序

python - 同一个迭代器的多个迭代器(使用枚举),这是怎么回事?

python - 使用 sum 代替 bfill 或 ffill 重新索引 Pandas DataFrame

python - PyCharm 的控制台因 "big"个对象而挂起