'pandas pivot data Cols to rows and rows to cols

I am using python and pandas have tried a variety of attempts to pivot the following (switch the row and columns)

Example: A is unique

         A       B   C   D     E... (and so on)
     [0] apple   2   22  222
     [1] peach   3   33  333
     [N] ... and so on

And I would like to see

    ?  ?       ?     ?   ... and so on
    A  apple   peach
    B  2       3
    C  22      33
    D  222     333
    E 
    ... and so on

I am ok if the columns are named after the col "A", and if the first column needs a name, lets call it "name"

    name  apple   peach ...
    B     2       3
    C     22      33
    D     222     333
    E 
    ... and so on



Solution 1:[1]

try df.transpose() it should do the trick

Solution 2:[2]

Taking the advice from the other posts, and a few other tweaks (explained in line) here is what worked for me.

    # get the key column that will become the column names.
    # add the column name for the existing columns

    cols = df['A'].tolist()
    cols.append('name')

    # Transform
    df = df.T
    
    # the transform takes the column, and makes it an index column.
    # need to add it back into the data set (you might want to drop 
    # the index later to get rid if it all together)
    df['name'] = df.index

    # now to rebuild the columns and move the new "name" column to the first col
    df.columns = cols
    cols = df.columns.tolist()
    cols = cols[-1:] + cols[:-1]
    df = df[cols]

    # remove the row, (was the column we used for the column names
    df = df.iloc[1:, :]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 osmanmo
Solution 2 QuentinJS