简体   繁体   中英

python pandas merge pivot tables

I created 9 pivot tables:

they all look like this

          2015
NR_V    
  0    20.000000
  1    20.405677
  2    35.982625
  3    50.475167
  4    61.578472

I want to merge them all by NR_V the way I would merge normal tables, but I think that empty line is throwing it off.

I tried this

dfs = [p_2009, p_2010, p_2011, p_2012, p_2013, p_2014, p_2015 ]
merge = partial(pd.merge, on=['NR_V'], how='outer')

result = dfs[0]
     for df in dfs[1:]:
     result = merge(result, df)

But I get an error "KeyError: 'NR_V'".

Try using pd.concat

dfs = [p_2009, p_2010, p_2011, p_2012, p_2013, p_2014, p_2015 ]
pd.concat(dfs, axis=1)

在此输入图像描述


response to comment
If you want to get rid of empty space

pd.concat(dfs, axis=1).reset_index()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM