简体   繁体   中英

Pivot Pandas DataFrame switching Index and Column Names

I have a table that was generated like this, to my understanding the index to the table is the (APPLE,BANANA,PEAR,ORANGE)

           INFO
APPLE      YUMMY
BANANA     5                                           
PEAR       GREEN                                            
ORANGE     {COLOR:ORANGE}                                               

I want to convert(pivot) my table to look like

       APPLE   BANANA   PEAR     ORANGE
INFO   YUMMY   5        GREEN    {COLOR:ORANGE} 

Where INFO is the only index and there are 4 columns

How would I be able to pivot this table? (with pivot() / pivot_table() im assuming)

You can use the built in DataFrame.transpose -- your transformation will look as follows:

In [1]: df
Out[1]:
                  INFO
APPLE            YUMMY
BANANA               5
PEAR             GREEN
ORANGE  {COLOR:ORANGE}

In [2]: df_transposed = df.transpose()

In [3]: df_transposed
Out[3]:
      APPLE BANANA   PEAR          ORANGE
INFO  YUMMY      5  GREEN  {COLOR:ORANGE}

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