我正在将逻辑转换为python,需要一些帮助。
如果A列货币成本CAD,那么我需要将所有列(D列,E列,G列)乘以0.7,这些列的名称中包含字符串“==”,如果A列货币== USD,那么D列,E列和G列将不会发生变化。
请注意,这是一个示例,有50多个列的名称中包含cost。
我尝试了下面的代码,但不起作用:
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发布于 2019-11-01 22:33:49
您想要loc访问:
df.loc[df['currency']=='cad', df.columns.str.endswith('_cost')] *= 0.7输出:
currency name prim_cost sales total_cost
0 cad a 0.7 1 2.8
1 cad b 3.5 2 2.1
2 usd c 7.0 3 6.0
3 usd d 9.0 4 7.0发布于 2019-11-01 22:29:31
筛选器选择列
df.loc[df.currency==1,['p','s','o']]*=0.7https://stackoverflow.com/questions/58660851
复制相似问题