0

I have a dataframe from "df.groupby("symbol").tail(2)" such as below

ID symbol prices
0 BNBBTC 0.009545
1 BNBBTC 0.009455
2 ONEUSDT 0.220050
3 ONEUSDT 0.220055

and I would like to transpose/pivot the data to get

ID symbol Last_Price Current_Price
0 BNBBTC 0.009545 0.009455
1 ONEUDST 0.220050 0.220055

Is it achievable with Pandas?

Thanks

1 Answer 1

2

Maybe you can use groupby + agg:

new_df = df.groupby('symbol')['prices'].agg(Last_Price='first', Current_Price='last').reset_index()

Output:

>>> new_df
    symbol  Last_Price  Current_Price
0   BNBBTC    0.009545       0.009455
1  ONEUSDT    0.220050       0.220055
Sign up to request clarification or add additional context in comments.

4 Comments

You might add a solution that computes the same directly from the previous object (OP uses already df.groupby("symbol").tail(2), so this would repeat it twice) ;)
I'm not quite sure what you mean @mozway - how about you just write it out in your own answer? :)
@richardec no time now! :O
Mozway, is partially right.. In my example, I showed only 2 rows. but infact I will have at least 2 rows per symbol. I was user tail(2) to get the last two per group.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.