2

I have two data frames that I want to combine into one. I want to combine two columns together.

                                      0
data_1_circulating_supply      17584875
data_2_circulating_supply   1.05209e+08
data_3_circulating_supply   41432141931
data_4_circulating_supply   6.08515e+07
data_5_circulating_supply   9.06245e+08
data_6_circulating_supply      17668725
data_7_circulating_supply   1.41175e+08
data_8_circulating_supply   1.99636e+09
data_9_circulating_supply   1.92156e+10
data_10_circulating_supply  6.66821e+10

                 0
data_1_symbol    BTC
data_2_symbol    ETH
data_3_symbol    XRP
data_4_symbol    LTC
data_5_symbol    EOS
data_6_symbol    BCH
data_7_symbol    BNB
data_8_symbol   USDT
data_9_symbol    XLM
data_10_symbol   TRX

I want the new dataframe to look like this:

BTC    17584875
ETH    1.05209e+08
XRP    41432141931
LTC    6.08515e+07
EOS    9.06245e+08
BCH    17668725
BNB    1.41175e+08
USDT   1.99636e+09
XLM    1.92156e+10
TRX    6.66821e+10

I cannot make it to work.

rakamakafo
  • 159
  • 8
  • 2
    Possible duplicate of [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – It_is_Chris Mar 12 '19 at 20:28
  • 1
    If your index numbers never go higher than 2 digits: `data_99_` then you can just `pd.merge(df, df2, left_on=df.index.str[:7], right_on=df2.index.str[:7])` if it does wee need more information as to the merge fields – It_is_Chris Mar 12 '19 at 20:36
  • Look at using .str.rsplit – Scott Boston Mar 12 '19 at 20:54

2 Answers2

2

Reshape each index so that there is only the numbers left:

df1.index = df1.index.str.split('_', n=2).str[1]

df2.index = df2.index.str.split('_', n=2).str[1]

Then put the two frames together.

pd.concat([df2, df1], axis=1)

       0             0
1    BTC  1.758488e+07
2    ETH  1.052090e+08
3    XRP  4.143214e+10
4    LTC  6.085150e+07
5    EOS  9.062450e+08
6    BCH  1.766872e+07
7    BNB  1.411750e+08
8   USDT  1.996360e+09
9    XLM  1.921560e+10
10   TRX  6.668210e+10
run-out
  • 2,806
  • 1
  • 7
  • 19
0

seems that your two dataframes have perfect incremental index. So if the same rule apply for rows bigger than 10th, you can just apply:

# [df1, df2]: your two dataframes
# axis=1: horizontally
# ignore_index=True: we don't need your previous indexes as row matches each other anyway

result = pd.concat([df1, df2], axis=1, ignore_index=True, sort=False)
Hubert Dudek
  • 1,063
  • 8
  • 18