我有两个数据帧,records_df和price_df,它们的索引是相同的
price_df:
index product price/product
s1 a 34
s1 b 34
s1 c 23
s2 a 12
s2 b 14
s2 c 65
s3 a 33
s3 b 65
s3 c 54
records_df:
index Name Number Product Sum of Amount of Products Day
s1 x 1234 a 50
s1 x 1234 a 50
s1 x 1234 a 50
s1 x 1234 a 50
s1 x 1234 a 50
s2 y 4321 b 30
s3 z 3219 c 40
s3 z 3219 d 40
我想要做的是将每个产品的价格插入到每天产品数量的总和之后,并将两个数据框合并在一起,结果将是:
index Name Number Product Sum of Amount of Products Day Price/product
s1 x 1234 a 50 34
s1 x 1234 a 50 34
s1 x 1234 a 50 34
s1 x 1234 a 50 34
s2 y 4321 b 30 34
s3 z 3219 c 40 14
s3 z 3219 b 40 65
我尝试过合并和连接,但不起作用。基本上,我尝试的是先对索引执行vlookup,然后对产品执行另一次vlookup。如何在python中创建嵌套的vlookup?
发布于 2018-07-22 02:40:55
IIUC,您可以只使用具有相同索引的loc
df2 = df2.set_index(['index', 'Product'])
df = df.set_index(['index', 'product'])
然后
df2.loc[:, 'price/product'] = df['price/product']
Name Number Sum_of_Amount_of_Products_Day price/product
index Product
s1 a x 1234 50 34.0
a x 1234 50 34.0
a x 1234 50 34.0
a x 1234 50 34.0
a x 1234 50 34.0
s2 b y 4321 30 14.0
s3 c z 3219 40 54.0
d z 3219 40 NaN
https://stackoverflow.com/questions/51458991
复制相似问题