2

I am trying to make a pandas dataframe using 2 paramters as columns. But it makes a dataframe transpose of what I need. I have a and b as column parameters as follows:

a=[1,2,3,4,5]
b=[11,22,33,44,55]

pd.DataFrame([a,b])

This gives the following dataframe:


    0   1   2   3   4 
0   1   2   3   4   5
1   11  22  33  44  55

However, I want the dataframe as:

    0   1 
0   1   11
1   2   22
2   3   33
3   4   44
4   5   55
lsr729
  • 752
  • 2
  • 11
  • 25

2 Answers2

2

One solution is transpose by DataFrame.T:

df = pd.DataFrame([a,b]).T

Or use zip:

df = pd.DataFrame(list(zip(a, b)))
#pandas 0.24+
#df = pd.DataFrame(zip(a, b))

print (df)
   0   1
0  1  11
1  2  22
2  3  33
3  4  44
4  5  55

Also is possible specify column names by dictionary:

df = pd.DataFrame({'a':a, 'b':b})
print (df)
   a   b
0  1  11
1  2  22
2  3  33
3  4  44
4  5  55
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
2

Dataframe has transpose function. The property T is an accessor to the method transpose()

pd.DataFrame([a,b]).T

or

pd.DataFrame([a,b]).transpose()