python - 如何实现涉及在多列上乘以数字的“if-else”条件
问题描述
我正在将逻辑转换为 python,需要一些帮助。
如果列 A [货币] == CAD,那么我需要将所有列(列 D、E、G)乘以 0.7,这些列的名称中包含字符串“成本”,如果列 A [货币] == USD,那么会有对于 col D、E 和 G 保持不变。
请注意,这是一个示例,名称中有超过 50 个列的成本。
我尝试了以下代码但没有用:
cad_cols = df.filter(regex='cost').columns
df[cad_cols] = np.where(df['currency']=="CAD",*0.7)
currency name address prim_cost sec_cost sales overall_cost
cad a x 1 8 1 4
cad b x 5 3 2 3
usd d x 7 2 3 6
usd e x 9 4 4 7
解决方案
筛选选择列
df.loc[df.currency==1,['p','s','o']]*=0.7
推荐阅读
- python - 从 Python 运行 MATLAB 可执行文件:找不到 9.1 版的 matlab 运行时
- r - 在 R 中不使用 iptools 反向跟踪 ip 地址
- python - 带有 2 个日期参数的 argparse
- c# - 发布 WPF 桌面应用程序后无法读取我的项目中的文件
- django - 我正在优化我的 django 项目后端,但是在包含 ForeignKey 的“list_display”上遇到了很多问题
- python - Python 通过请求更改 instagram bio
- python - 如何指定不同的 token_policies 并根据用户在 hashcorp vault 中的输入指定租约期限?
- javascript - 在 reactjs 中将 searchParam 数据从一个组件发送到另一个组件
- rust - Rust actix_web::main “预期 `std::result::Result<(), std::io::Error>` 因为返回类型”但建议的类型不起作用
- flutter - 如何在页面路由中使用颤振抽屉菜单?