给定下面的代码,我想计算三个额外的列,称为&;Expected Sales {letter}&;将A、B和C的每一列乘以价格列。但是要做到这一点,我想传递一个列名列表,这样只有那些在列名中带有"Group"的列名才会相乘(这意味着其他什么都不做)
data = [(3,5,7), (2,4,6), (5,8,9.2)]
df = pd.DataFrame(data, columns = ['Group A','Group B','Group C'])
df['Fruit'] = ('apples', 'bananas', 'pears')
df['Price'] = (1, 0.5, 2)
df['Other'] = ("blah1", "blah2", "blah3")
使用filter
来选择列,然后使用rename
来替换"Group"string by "Expected Sales"
df1 = (df.filter(like='Group').mul(df['Price'], axis=0)
.rename(columns=lambda x: x.replace('Group', 'Expected Sales')))
out = pd.concat([df, df1], axis=1)
输出:
>>> out
Group A Group B Group C Fruit Price Other Expected Sales A Expected Sales B Expected Sales C
0 3 5 7.0 apples 1.0 blah1 3.0 5.0 7.0
1 2 4 6.0 bananas 0.5 blah2 1.0 2.0 3.0
2 5 8 9.2 pears 2.0 blah3 10.0 16.0 18.4