pandas创建引用某些列和列标题的新列

时间:2018-05-03 13:29:59

标签: python pandas

这是我目前的数据框架。我想添加两个名为PriceError和CostError的新列。

Price 1  Price2 Cost1 Cost2   %Price %Cost PriceError  CostError
1        1      3    6        0     100                      
2        4      3    3        100   0           

df [' PriceError']应该等于以下字符串Price1 is 2 and Price1 is 4. The %Price is 100

df [' CostError']应该等于以下字符串Cost1 is 3 and Cost1 is 6. The %Price is 100

我只返回那些因为其他两个低于50%这是我的错误阈值。

SO PriceError硬编码为" Price1为df['Price1'],Price2为df['Price2'],%Price为df['%Price']"。

我想以某种方式写出PriceError应该只查看列price1,price2和%price并写入字符串。

1 个答案:

答案 0 :(得分:0)

这是你的目标:

df['PriceError'] = df.filter(like='Price').apply(lambda x: 'Price1 is {} and Price2 is {}. The %Price is {}'.format(*x), axis = 1)

为避免硬编码,这是解决方法

prices = df.filter(like='Price')

df['PriceError'] = prices.apply(lambda x: '{3} is {0} and {4} is {1}. The {5} is {2}.'.format(*(list(x)+prices.columns.tolist())), axis=1)