简体   繁体   中英

How to swap rows to columns in such a way?

Here is the current dataset in a dataframe:

在此处输入图像描述

Now I would like to convert it to this way:

在此处输入图像描述

Kindly assist

This can be done more straight-forward.

df = df.melt("Date", value_name="Strike1").reset_index(drop=True)
df.pop("variable") # delete an additional column that .melt creates

>>> df
           Date  Strike1
    0  1-Jan-20     1000
    1  2-Jan-20      300
    2  1-Jan-20      700
    3  2-Jan-20      896
    4  1-Jan-20      600
    5  2-Jan-20      259

If you want to sort by Date , you can do this afterwards:

df = df.sort_values(by=["Date"]).reset_index(drop=True)
>>> df
           Date  Strike1
    0  1-Jan-20     1000
    1  1-Jan-20      700
    2  1-Jan-20      600
    3  2-Jan-20      300
    4  2-Jan-20      896
    5  2-Jan-20      259

.reset_index() is used to avoid having an unordered index as a result of the new ordering given to the dataframe. It is totally optional and you may or may not want this, depending on what you're doing this for.

>>> pd.Series(df.loc[:, df.columns.str.startswith("strike")].values.tolist(), index=df["Date"], name="Strike").explode().to_frame().reset_index() Date Strike 0 2020-1-1 1000 1 2020-1-1 700 2 2020-1-1 600 3 2020-1-2 300 4 2020-1-2 896 5 2020-1-2 259

With set_index and stack :

import pandas as pd

df = pd.DataFrame({'Date': ['1-Jan-20', '2-Jan-20'],
                   'strike1': [1000, 300],
                   'strike2': [700, 896],
                   'strike3': [600, 259]})

out = df.set_index('Date').stack().droplevel(1, 0).reset_index(name='Strike')
print(out)

out :

       Date  Strike
0  1-Jan-20    1000
1  1-Jan-20     700
2  1-Jan-20     600
3  2-Jan-20     300
4  2-Jan-20     896
5  2-Jan-20     259

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM