Python 以列格式将一个表的输出合并到另一个表
我有一个数据框,看起来像这样:Python 以列格式将一个表的输出合并到另一个表,python,pandas,list,dataframe,metadata,Python,Pandas,List,Dataframe,Metadata,我有一个数据框,看起来像这样: Plant Date Item unitsSold 1 10-oct A 15 1 11-Oct A 20 1 12-Oct A 10 2 10-Oct B 19 2
Plant Date Item unitsSold
1 10-oct A 15
1 11-Oct A 20
1 12-Oct A 10
2 10-Oct B 19
2 11-Oct A 20
2 12-Oct C 10
Plant Date Item unit Price
1 10-Oct A,B 10 ---That means both A and B have same unit price
1 11-Oct A 14
1 12-Oct A,B,C 10 ----That means both A, B and C have same price
2 10-oct A,B,C 15
2 11-Oct A 10
3 12-Oct A,C 20
Plant Date Item unit Price A.UnitsSold B.Unitssold C.unitssold
1 10-Oct A,B 10 15 0 0 --Since only a was sold on 10-oct
1 11-Oct A 14 20 0 0
1 12-Oct A,B,C 10 10 0 0
2 10-oct A,B,C 15 0 19 0
2 11-Oct A 10 20 0 0
3 12-Oct A,C 20 0 0 10
另一个数据集如下所示:
Plant Date Item unitsSold
1 10-oct A 15
1 11-Oct A 20
1 12-Oct A 10
2 10-Oct B 19
2 11-Oct A 20
2 12-Oct C 10
Plant Date Item unit Price
1 10-Oct A,B 10 ---That means both A and B have same unit price
1 11-Oct A 14
1 12-Oct A,B,C 10 ----That means both A, B and C have same price
2 10-oct A,B,C 15
2 11-Oct A 10
3 12-Oct A,C 20
Plant Date Item unit Price A.UnitsSold B.Unitssold C.unitssold
1 10-Oct A,B 10 15 0 0 --Since only a was sold on 10-oct
1 11-Oct A 14 20 0 0
1 12-Oct A,B,C 10 10 0 0
2 10-oct A,B,C 15 0 19 0
2 11-Oct A 10 20 0 0
3 12-Oct A,C 20 0 0 10
现在我希望我的输出如下所示:
Plant Date Item unitsSold
1 10-oct A 15
1 11-Oct A 20
1 12-Oct A 10
2 10-Oct B 19
2 11-Oct A 20
2 12-Oct C 10
Plant Date Item unit Price
1 10-Oct A,B 10 ---That means both A and B have same unit price
1 11-Oct A 14
1 12-Oct A,B,C 10 ----That means both A, B and C have same price
2 10-oct A,B,C 15
2 11-Oct A 10
3 12-Oct A,C 20
Plant Date Item unit Price A.UnitsSold B.Unitssold C.unitssold
1 10-Oct A,B 10 15 0 0 --Since only a was sold on 10-oct
1 11-Oct A 14 20 0 0
1 12-Oct A,B,C 10 10 0 0
2 10-oct A,B,C 15 0 19 0
2 11-Oct A 10 20 0 0
3 12-Oct A,C 20 0 0 10
有人能告诉我如何得到出售的单位栏吗。注意-可以有任意数量的样式df1.merge(df2,左上='lkey',右上='rkey')
您可以从以下合并代码或问题中获得想法:
可能有帮助:stackoverflow.com/questions/37113173/…
与列合并
df_new =pd.concat([df1, df2], axis=1)
是的,我试过m=df1.set_index('item').tn=final['Date']].assign(**final['item'].str.get_dummies(',')).set_index('Date')final1=final.set_index('Date').assign(m.mul(n).sum(1))----但我得到的是要得到的总数,请在你的帖子中包含这些信息,呃。忘了提到你应该提供一个更好的解释来说明你想要如何合并它们。我想你可以做
pd.concat([df2,df2[“Item”].str.split(',',,expand=True)。替换({None:np.NaN}),axis=1)
来扩展你的Item
列。从那里你可以开始合并。