Pandas 合并两个数据帧时复制行
我有两个数据集,我在下面添加了截图 Macro.csv(图像中的完整列列表) Train.csv 在macro.csv中,您可以看到我一年中每个月有一条记录,而在train.csv中,我一年中每个月有3条记录。Pandas 合并两个数据帧时复制行,pandas,dataframe,merge,concat,Pandas,Dataframe,Merge,Concat,我有两个数据集,我在下面添加了截图 Macro.csv(图像中的完整列列表) Train.csv 在macro.csv中,您可以看到我一年中每个月有一条记录,而在train.csv中,我一年中每个月有3条记录。 我想将Macro.csv与Train.csv合并到所有列字段中,以便一年中每个月都有3条记录,其中所有列都在Macro.csv中。将两个csv加载到数据框中并使用合并: res = pd.merge(macro_df, train_df, on=['Year','Month'])
我想将Macro.csv与Train.csv合并到所有列字段中,以便一年中每个月都有3条记录,其中所有列都在Macro.csv中。将两个csv加载到数据框中并使用合并:
res = pd.merge(macro_df, train_df, on=['Year','Month'])
看看熊猫吧。图片链接不是个好主意。我还添加了代码@fireandwind
Year Month ProductCategory Sales(In ThousandDollars)
0 2009 1 WomenClothing 1755.0
1 2009 1 MenClothing 524.0
2 2009 1 OtherClothing 936.0
3 2009 2 WomenClothing 1729.0
4 2009 2 MenClothing 496.0
5 2009 2 OtherClothing 859.0
6 2009 3 WomenClothing 2256.0
7 2009 3 MenClothing 542.0
8 2009 3 OtherClothing 921.0
9 2009 4 WomenClothing 2662.0
10 2009 4 MenClothing 669.0
11 2009 4 OtherClothing 914.0
12 2009 5 WomenClothing 2732.0
13 2009 5 MenClothing 650.0
14 2009 5 OtherClothing 989.0
15 2009 6 WomenClothing 2220.0
16 2009 6 MenClothing 607.0
17 2009 6 OtherClothing 932.0
18 2009 7 WomenClothing 2164.0
19 2009 7 MenClothing 575.0
20 2009 7 OtherClothing 901.0
21 2009 8 WomenClothing 2371.0
22 2009 8 MenClothing 551.0
23 2009 8 OtherClothing 865.0
24 2009 9 WomenClothing 2421.0
25 2009 9 MenClothing 579.0
26 2009 9 OtherClothing 819.0
27 2009 10 WomenClothing 2579.0
28 2009 10 MenClothing 610.0
29 2009 10 OtherClothing 914.0
.. ... ... ... ...
186 2014 3 WomenClothing 4268.0
187 2014 3 MenClothing 702.0
res = pd.merge(macro_df, train_df, on=['Year','Month'])