Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas/Python – Dataframe transpose/pivot

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading