合并两个数据框时复制行

时间:2018-07-15 09:20:59

标签: pandas dataframe merge concat

我有两个数据集,我在下面添加了屏幕截图

Macro.csv(图像中的完整列列表) Macro.csv

  Year-Month  Year  Month  Monthly Nominal GDP Index (inMillion$)  \
0   2009 - Jan  2009      1                            14421.752895   
1   2009 - Feb  2009      2                            14389.200466   
2   2009 - Mar  2009      3                            14340.701639   
3   2009 - Apr  2009      4                            14326.815525   
4   2009 - May  2009      5                            14345.904809   
5   2009 - Jun  2009      6                            14348.530666   
6   2009 - Jul  2009      7                            14317.372922   
7   2009 - Aug  2009      8                            14424.284901   
8   2009 - Sep  2009      9                            14410.777177   
9   2009 - Oct  2009     10                            14596.605447   
10  2009 - Nov  2009     11                            14590.432252   
11  2009 - Dec  2009     12                            14512.495301   
12  2010 - Jan  2010      1                            14613.991481   
13  2010 - Feb  2010      2                            14652.880272   
14  2010 - Mar  2010      3                            14776.317248   
15  2010 - Apr  2010      4                            14844.531193   
16  2010 - May  2010      5                            14875.698436   
17  2010 - Jun  2010      6                            14945.570371   
18  2010 - Jul  2010      7                            15019.235263   
19  2010 - Aug  2010      8                            15036.277627   
20  2010 - Sep  2010      9                            15117.467110   
21  2010 - Oct  2010     10                            15219.059036   
22  2010 - Nov  2010     11                            15185.628947   
23  2010 - Dec  2010     12                            15285.936017   
24  2011 - Jan  2011      1                            15149.136163   
25  2011 - Feb  2011      2                            15161.578331   
26  2011 - Mar  2011      3                            15404.398506   
27  2011 - Apr  2011      4                            15488.004864   

Train.csv

Train.csv

   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

在macro.csv中,您可以看到我每年一年的每个月都有一条记录,而在train.csv中,我一年中的每个月都有3条记录。 我想将Macro.csv与所有列字段合并到Train.csv中,以便我一年中每个月都有3条记录,其中所有列都在Macro.csv中。

1 个答案:

答案 0 :(得分:1)

将两个csv都加载到一个数据帧中并使用merge:

res = pd.merge(macro_df, train_df, on=['Year','Month'])

查看大熊猫merge