简体   繁体   中英

How to unstack a dataframe in pandas?

I am very new to programming languages. I am trying pandas and python. I have a csv file that I want to turn the table. The original table looks like:

    W0  W1  W2
P1  11  12  10
P2   7   6   3
P3   7  11   8
P4  12   8  13
P5   8   5  13

I want to turn the table so that the P1-Pn would be my first row and W0-Wn be my column. I tried several codes but none worked.

Your question is a little unclear.

If you mean you want the P values as the Columns and W values as index you can transpose the dataframe:

df.T
    P1  P2  P3  P4  P5
W0  11   7   7  12   8
W1  12   6  11   8   5
W2  10   3   8  13  13

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