1

I want to concat those 2 dataframe:

         circulating_supply
currency                            
BCH                         18225550
BTC                         18163250
ETH                        109296900
QASH                       350000000
XRP                      43653780000          
           circulating_supply
currency                    
BCH             1.822718e+07
BTC             1.816522e+07
ETH             1.093100e+08
QASH            3.500000e+08
XRP             4.365378e+10

my code:

pd.concat([supp_bal, supp_prev], axis=1, sort=True)

The output:

             circulating_supply  circulating_supply
BCH                     1.822718e+07                 NaN
BCH                              NaN        1.822555e+07
BTC                     1.816522e+07                 NaN
BTC                              NaN        1.816325e+07
ETH                     1.093100e+08                 NaN
ETH                              NaN        1.092969e+08
QASH                    3.500000e+08                 NaN
QASH                             NaN        3.500000e+08
XRP                     4.365378e+10                 NaN
XRP                              NaN        4.365378e+10

I would like the same output without double index and NaN. Any contribution would be appreciated.

delalma
  • 838
  • 3
  • 12
  • 24

1 Answers1

0

Join the dataframes (which does a left join on the indices by default) and specify a suffix for each column since they have the same name.

>>> df1.join(df2, lsuffix='_1', rsuffix='_2')
          circulating_supply_1  circulating_supply_2
currency                                            
BCH                   18225550          1.822718e+07
BTC                   18163250          1.816522e+07
ETH                  109296900          1.093100e+08
QASH                 350000000          3.500000e+08
XRP                43653780000          4.365378e+10

Also review: Pandas Merging 101

Alexander
  • 105,104
  • 32
  • 201
  • 196
  • thanks, I have `nan` in the 2nd column whichever dataframe i put in df1 or df2. Is it possible to have an issue with the indexes? They look the same but are different? One of the dataframe is an import from an xls file... – delalma Jan 17 '20 at 07:32
  • 1
    1) Make sure that `currency` is the index and not a column in the dataframe. 2) Ensure that the names match exactly and do not contain spaces, e.g. `BCH ` vs `BCH`. The sample above works just fine with your sample data. – Alexander Jan 17 '20 at 07:36