合并2个变量时,concat不会按照我想要的方式打印它们

时间:2018-09-11 04:27:39

标签: python pandas dataframe merge concat

当前程序如下:

a1

      Salary
Year        
2007    2750
2008    2850
2009    2700
2010    2900
2011    3000
2012    3050
2013    3050
2014    3200
2015    3300
2016    3300

b1

      GDP for Year
Year              
2007      271249.8
2008      271980.4
2009      279858.0
2010      322361.1
2011      346649.0
2012      361365.9
2013      378531.6
2014      390447.7
2015      408096.6
2016      410271.9

当我合并它时,它显示:

pd.concat([a1, b1], axis=1, ignore_index=False)

      Salary  GDP for Year
Year                      
2007  2750.0           NaN
2008  2850.0           NaN
2009  2700.0           NaN
2010  2900.0           NaN
2011  3000.0           NaN
2012  3050.0           NaN
2013  3050.0           NaN
2014  3200.0           NaN
2015  3300.0           NaN
2016  3300.0           NaN
2007     NaN      271249.8
2008     NaN      271980.4
2009     NaN      279858.0
2010     NaN      322361.1
2011     NaN      346649.0
2012     NaN      361365.9
2013     NaN      378531.6

我希望将其打印为:

        Salary   GDP For Year
Year    
2007    2750 .   271249.8
2008    2850 .   271980.4
2009    2700 .  279858.0
2010    2900 .  322361.1
2011    3000 .   346649.0
2012    3050 .   361365.9
2013    3050 .   378531.6
2014    3200 .   390447.7
2015    3300 .   408096.6
2016    3300 .   410271.9

然后在那之后,我想绘制一个图形并创建一个比较,以比较两条线在单个图形中的相互比较。

我已经尝试过,合并,加入,合并,但到目前为止没有任何帮助。

2 个答案:

答案 0 :(得分:1)

数据框的索引之一是字符串类型,而另一个是整数

rename

a1.rename(int, inplace=True)
b1.rename(int, inplace=True)
pd.concat([a1, b1], axis=1)

      Salary  GDP for Year
Year                      
2007    2750      271249.8
2008    2850      271980.4
2009    2700      279858.0
2010    2900      322361.1
2011    3000      346649.0
2012    3050      361365.9
2013    3050      378531.6
2014    3200      390447.7
2015    3300      408096.6
2016    3300      410271.9

分配给index

a1.index = a1.index.astype(int)
b1.index = b1.index.astype(int)
pd.concat([a1, b1], axis=1)

答案 1 :(得分:0)

尝试:

library(dplyr)
output <- data %>%

  # convert DocDate to a date
  mutate(DocDate = as.Date(DocDate,'%m/%d/%Y')) %>%

  # For each Material...
  group_by(Material) %>% 

  # just keep the line(s) with the first date...
  filter(DocDate == min(DocDate)) %>% ungroup() %>% 

  # and combine fields
  mutate(`Name/address/Unit Price` = paste(Name, Address, Unit_Price, sep = "/")) %>%

  # just the requested columns
  select(Material, `Name/address/Unit Price`)

output
# A tibble: 1 x 2
  Material `Name/address/Unit Price`
     <int> <chr>                    
1  1258486 FEHLIG BROS BOX/asd/8.95