python - 如何将此数据框转换为所需的设计?
问题描述
我有一张如下表:
我必须比较personA和PersonB,很难用肉眼判断personA的花费是否少于或多于personB,因此我想动态(使用pandas)为每个amount_spent属性添加一个列,其中IF personA花费少于那个类别的人 B 然后把 Y... 我不确定如何在 pandas 中实现以下目标:
有人可以帮忙吗?
解决方案
假设列名结构相同,可以np.where
这样使用:
x=['food','clothes','vehicles','gadgets']
for i in x:
df['amount_spend_'+i+'_tag']=np.where(df['amount_spend_'+i+'_PersonA']<df['amount_spend_'+i+'_PersonB'],"Y","")
推荐阅读
- java - Android:如何播放音频文件?
- python - 如何在linux上停止由python启动的外部程序
- python - 使用字符串而不是列表来存储多个值会起作用吗?
- java - NetBeans 平台:如何禁用特定选项对话框类别?
- javascript - Nodejs环回TCP延迟1-2ms?
- github - Github:如何从发布分支自动合并到开发分支?
- javascript - 如何在 react-bootstrap 中实现溢出隐藏?
- c++ - 无法删除在 Qt6 中自动检测到的重复的 Android 6.0.0 Clang 编译器套件
- asp.net-core - Blazor Web 程序集 - 各种 OIDC 身份验证路径之间的区别
- android - 如何在Android中处理按钮