python - 如何为熊猫数据框的特定列添加值?
问题描述
我必须对 pandas DataFrame 的特定列执行相同的算术运算。我这样做
c.loc[:,'col3'] += cons
c.loc[:,'col5'] += cons
c.loc[:,'col6'] += cons
应该有一种更简单的方法可以在一次操作中完成所有这些操作。我的意思是col3,col5,col6
在一个命令中更新。
解决方案
pd.DataFrame.loc
标签索引接受列表:
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=['A', 'B', 'C'])
df.loc[:, ['B', 'C']] += 10
print(df)
A B C
0 1 12 13
1 4 15 16
推荐阅读
- html - 在 React 中,onKeyUp 和 onKeyUpCapture(以及 onKeyDown/Capture)有什么区别?
- php - 是否可以针对大型 Web 表单中的每个输入字段更改触发查询?
- json - 如何将 JSON 字符串转换为数字?
- ansible - Ansible 包含角色中的文件
- ajax - 在 laravel 中使用 ajax 将 base64 图像数据发送到控制器
- soap - 使用 Apache Camel 使用和生成 SOAP 到 SOAP
- ios - 我可以查看 iOS 应用程序是否已更新?
- css - 如何在 React 引导样式组件中覆盖 -btn-primary 类
- uitabbarcontroller - Swift 5 中 .XIB 文件中的操作表显示
- python - Monkey Patch 实例方法在 Python 多处理中不起作用