How do I transpose dataframe in pandas without index?

后端 未结 2 769
旧时难觅i
旧时难觅i 2020-11-27 05:53

Pretty sure this is very simple.

I am reading a csv file and have the dataframe:

Attribute    A   B   C
a            1   4   7
b            2   5   8         


        
相关标签:
2条回答
  • 2020-11-27 06:33

    It works for me:

    >>> data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}
    >>> df = pd.DataFrame(data, index=['a', 'b', 'c'])
    >>> df.T
       a  b  c
    A  1  2  3
    B  4  5  6
    C  7  8  9
    
    0 讨论(0)
  • 2020-11-27 06:57

    You can set the index to your first column (or in general, the column you want to use as as index) in your dataframe first, then transpose the dataframe. For example if the column you want to use as index is 'Attribute', you can do:

    df.set_index('Attribute',inplace=True)
    df.transpose()
    

    Or

    df.set_index('Attribute').T
    
    0 讨论(0)
提交回复
热议问题