Convert column to row in Python Pandas

后端 未结 3 1630
借酒劲吻你
借酒劲吻你 2020-11-29 06:25

I have the following Python pandas dataframe:

     fruits | numFruits
---------------------
0  | apples |   10
1  | grapes |   20
2  |  figs  |   15
<         


        
相关标签:
3条回答
  • 2020-11-29 06:59

    You can use transpose api of pandas as follow:

    df.transpose()
    

    Considering df as your pandas dataframe

    0 讨论(0)
  • 2020-11-29 07:09
    pd.DataFrame([df.numFruits.values], ['Market 1 Order'], df.fruits.values)
    
                    apples  grapes  figs
    Market 1 Order      10      20    15
    

    Refer to jezrael's enhancement of this concept. df.numFruits.values.reshape(1, -1) is more efficient.

    0 讨论(0)
  • 2020-11-29 07:19

    You need set_index with transpose by T:

    print (df.set_index('fruits').T)
    fruits     apples  grapes  figs
    numFruits      10      20    15
    

    If need rename columns, it is a bit complicated:

    print (df.rename(columns={'numFruits':'Market 1 Order'})
             .set_index('fruits')
             .rename_axis(None).T)
                    apples  grapes  figs
    Market 1 Order      10      20    15
    

    Another faster solution is use numpy.ndarray.reshape:

    print (pd.DataFrame(df.numFruits.values.reshape(1,-1), 
                        index=['Market 1 Order'], 
                        columns=df.fruits.values))
    
                    apples  grapes  figs
    Market 1 Order      10      20    15
    

    Timings:

    #[30000 rows x 2 columns] 
    df = pd.concat([df]*10000).reset_index(drop=True)    
    print (df)
    
    
    In [55]: %timeit (pd.DataFrame([df.numFruits.values], ['Market 1 Order'], df.fruits.values))
    1 loop, best of 3: 2.4 s per loop
    
    In [56]: %timeit (pd.DataFrame(df.numFruits.values.reshape(1,-1), index=['Market 1 Order'], columns=df.fruits.values))
    The slowest run took 5.64 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000 loops, best of 3: 424 µs per loop
    
    In [57]: %timeit (df.rename(columns={'numFruits':'Market 1 Order'}).set_index('fruits').rename_axis(None).T)
    100 loops, best of 3: 1.94 ms per loop
    
    0 讨论(0)
提交回复
热议问题