【发布时间】:2020-11-25 15:46:53
【问题描述】:
df1:
Id Country P_Type Sales
102 Portugal Industries 1265
163 Portugal Office 1455
111 Portugal Clubs 1265
164 Portugal cars 1751
109 India House_hold 1651
104 India Office 1125
124 India Bakery 1752
112 India House_hold 1259
105 Germany Industries 1451
103 Germany Office 1635
103 Germany Clubs 1520
103 Germany cars 1265
df2:
Id Market Products Expenditure
123 Portugal ALL Wine 5642
136 Portugal St Wine 4568
158 India QA Housing 4529
168 India stm Housing 1576
749 Germany all Sports 4587
759 Germany sts Sports 4756
输出df:
Id Country P_Type Sales
102 Portugal Industries 1265
102 Portugal ALL Wine 5642
102 Portugal St Wine 4568
163 Portugal Office 1455
111 Portugal Clubs 1265
164 Portugal cars 1751
109 India House_hold 1651
109 India QA Housing 4529
109 India stm Housing 1576
104 India Office 1125
124 India Bakery 1752
112 India House_hold 1259
105 Germany Industries 1451
105 Germany all Sports 4587
105 Germany sts Sports 4756
103 Germany Office 1635
103 Germany Clubs 1520
103 Germany cars 1265
我需要附加两个数据框,但来自 df2 的行应该附加在 df1 中的特定位置。 例如,在 df2 中,前两行“市场”列属于葡萄牙,在我的 df1 中 国家葡萄牙的第一行 ID 是 102,它应该附加在具有相同 ID 的葡萄牙的第一行之后。 其他国家也一样。
【问题讨论】:
-
德文和德文不一样。除非你想要模糊匹配。此外,您只需要每个国家/地区的 df1 中的第一行,然后是 df2 中相应的市场行,然后是每个组的其余行。到现在为止?
-
@sammywemmy 我已经更新了问题,请调查一下。我在df2改到德国了。
标签: python python-3.x pandas python-2.7 dataframe