任何人都可以帮忙,我是Python的新手,所以请多多包涵。
我的数据看起来像这样,但是具有所有可用的区域信息。我正在尝试创建一个新列“实际价格”,该列会根据区域得出价格。至于每次入境,我对每个地区都有不同的价格。这可能吗。
data = [[1, 'EDF', 'Eastern', 400, 500, 300], [2, 'EDF', 'Southern', 200, 100, 300], [3, 'NPower',
'Eastern', 600, 500, 700]]
df = pd.DataFrame(data, columns = ['ID', 'Supplier', 'Region', 'Av Price', 'Eastern Price',
'Southern Price'])
df
IIUC,您可以df.lookup
在“地区”列的值中添加“价格”以匹配“价格按地区”的列名称后在此处执行以下操作:
m = df.loc[:,df.columns.str.endswith("Price")]
df['actual_Price'] = m.lookup(df.index,df['Region'].add(" Price"))
print(df)
ID Supplier Region Av Price Eastern Price Southern Price \
0 1 EDF Eastern 400 500 300
1 2 EDF Southern 200 100 300
2 3 NPower Eastern 600 500 700
actual_Price
0 500
1 300
2 500
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句