-2

i have two pandas frame and i want to get one

     asks_price  asks_qty exchange_name_ask
0      20156.51  0.000745          Coinbase
1      20156.52  0.050000          Coinbase
     bids_price  bids_qty exchange_name_bid
2      20153.28  0.000200          Coinbase
3      20153.27  0.051000          Coinbase


Get ====>

     asks_price  asks_qty exchange_name_ask     bids_price  bids_qty exchange_name_bid
0      20156.51  0.000745          Coinbase      20153.28  0.000200          Coinbase
1      20156.52  0.050000          Coinbase      20153.27  0.051000          Coinbase

ask_snapshot = ask_snapshot.groupby('asks_price')['asks_qty'].sum().reset_index()
bid_snapshot = bid_snapshot.groupby('bids_price')['bids_qty'].sum().reset_index()
ask_snapshot = ask_snapshot.sort_values(by='asks_price').reset_index()
bid_snapshot = bid_snapshot.sort_values(by='bids_price', ascending=False).reset_index()
ask = ask_snapshot.head(20)
bid = bid_snapshot.head(20)

snapshot = pd.concat([ask, bid], axis=1, join='inner')

in the snapshot the exchange_name column disappear, i dont know why Thanks

Nathan
  • 91
  • 6
  • 1
    Wouldn't [pandas.concat](https://pandas.pydata.org/docs/reference/api/pandas.concat.html) work for you? – jjramsey Sep 15 '22 at 12:46
  • 1
    Does this answer your question? [What are the 'levels', 'keys', and names arguments for in Pandas' concat function?](https://stackoverflow.com/questions/49620538/what-are-the-levels-keys-and-names-arguments-for-in-pandas-concat-functio) – Rabinzel Sep 15 '22 at 12:50
  • No i already try pandas.concat it doent work – Nathan Sep 15 '22 at 12:53
  • this is the most basic standard textbook concat which in every single tutorial is explained. concat to dataframes together along axis=1. what do you mean by right index ? both of your dataframes have the same index – Rabinzel Sep 15 '22 at 12:58
  • No in my exemple they have the same but not always . When i am trying to reset index before concat , the exchange name dissapear – Nathan Sep 15 '22 at 13:06
  • 1
    In your desired output is the left index...you said you want to have the right index? I guess you forgot to edit that too. – Rabinzel Sep 15 '22 at 13:10
  • Do you know a solution thanks – Nathan Sep 15 '22 at 13:12
  • How about you take a minute and read [how to ask](https://stackoverflow.com/help/how-to-ask) with a [MRE](https://stackoverflow.com/help/minimal-reproducible-example) and edit your question accordingly. We aren't here to guess what you maybe want or don't want. Change your question with accurate input and desired output and you will get the right answer in no time. – Rabinzel Sep 15 '22 at 13:21

2 Answers2

1

You can try this

snapshot = pd.concat([ask_snapshot.reset_index(drop=True),
    bid_snapshot.reset_index(drop=True)], axis=1)
Troll
  • 1,895
  • 3
  • 15
  • 34
1

The two columns exchange_name.. doesn't disappear when you use pandas.concat but they simply doesn't exist in the two dataframes passed as arguments.

Try this :

ask_snapshot = ask_snapshot.groupby(['asks_price', 'exchange_name_ask'], as_index=False)['asks_qty'].sum()
bid_snapshot = bid_snapshot.groupby(['bids_price', 'exchange_name_bid'], as_index=False)['bids_qty'].sum()
ask_snapshot = ask_snapshot.sort_values(by='asks_price').reset_index()
bid_snapshot = bid_snapshot.sort_values(by='bids_price', ascending=False).reset_index()
ask = ask_snapshot.head(20)
bid = bid_snapshot.head(20)

snapshot = pd.concat([ask, bid], axis=1)
Timeless
  • 22,580
  • 4
  • 12
  • 30