29

I have the following Python pandas dataframe:

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

I want:

                 apples | grapes | figs
-----------------------------------------
Market 1 Order |    10  |   20   |  15

I have looked at pivot(), pivot_table(), Transpose and unstack() and none of them seem to give me this. Pandas newbie, so all help appreciated.

thinwybk
  • 4,193
  • 2
  • 40
  • 76
Reise45
  • 1,163
  • 4
  • 18
  • 23
  • 1
    If you are interested about difference of performace, check this [question](http://stackoverflow.com/questions/41862406/performance-of-creating-new-dataframe) – jezrael Jan 25 '17 at 22:42

3 Answers3

28

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Hi ...In this question there are only 3 columns what If we have 10 columns and we need to retain 8 of them and just use the other 2 to reshape the data ? – yasin mohammed Apr 17 '17 at 10:34
  • It seems you need [`pivot_table`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.pivot_table.html), but without data hard answer. Maybe the best is create new question with sample data, desired output and what do you try (your code) – jezrael Apr 17 '17 at 14:07
  • I just created a new question please review. – yasin mohammed Apr 17 '17 at 14:11
10
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.

piRSquared
  • 285,575
  • 57
  • 475
  • 624
1

You can use transpose api of pandas as follow:

df.transpose()

Considering df as your pandas dataframe

Akash Desarda
  • 704
  • 8
  • 6