1

I am trying to create a DataFrame like this:

column_names= ["a", "b", "c"]
vals = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

df = pd.DataFrame(vals, columns=column_names)

Which results in the following DataFrame:

  a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

I suppose this is the expected result. However, I am trying to achieve this result:

  a  b  c
0  1  4  7
1  2  5  8
2  3  6  9

Where each nested list in vals corresponds to a whole column instead of a row.

Is there a way to get the above DataFrame without changing the way the data is passed to the constructor? Or even a method I can call to transpose the DataFrame?

ghood97
  • 161
  • 1
  • 12
  • Do you want also change index and columns names? after transpose? – jezrael Nov 24 '20 at 07:03
  • 1
    the output clearly shows that to be true. I dont think we need to close this question because problem is how to feed a row-wise list data of column-wise data to a pd.dataframe – Akshay Sehgal Nov 24 '20 at 07:04

4 Answers4

5

Just zip it:

df = pd.DataFrame(dict(zip(column_names, vals)))

Outputs:

   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9
Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
1

Use transpose(df.T):

In [3397]: df = df.T.reset_index(drop=True)

In [3398]: df.columns = column_names

In [3399]: df
Out[3399]: 
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

Try the column naming in a difference step -

column_names= ["a", "b", "c"]
vals = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

df = pd.DataFrame(vals).T
df.columns = column_names
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9

Or if you can use numpy, you can do it in one step -

import numpy as np

column_names= ["a", "b", "c"]
vals = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

df = pd.DataFrame(vals.T, columns=column_names)
print(df)
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9
Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
1

If use constructor simplier is use zip with unpacking list with *:

column_names= ["a", "b", "c"]
vals = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

df = pd.DataFrame(zip(*vals), columns=column_names)
print (df)
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9

Solutions if already was created DataFrame:

df = pd.DataFrame(vals, columns=column_names)

Use DataFrame.T and reassign columns with index values:

df1 = df.T
df1.columns, df1.index = df1.index, df1.columns
print (df1)

   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9

One line solution with transpose, DataFrame.set_axis and DataFrame.reset_index:

df1 = df.T.set_axis(column_names, axis=1).reset_index(drop=True)
print (df1)
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9

Or transpose only numpy array, thank you @Henry Yik:

 df.loc[:] = df.T.to_numpy()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252