How to convert list to row dataframe with Pandas

Federico Gentile picture Federico Gentile · Feb 13, 2017 · Viewed 71.4k times · Source

I have a list of items like this:

A = ['1', 'd', 'p', 'bab', '']

My goal is to convert such list into a dataframe of 1 row and 5 columns. If I type pd.DataFrame(A) I get 5 rows and 1 column. What should I do in order to get the result I want?

Answer

jezrael picture jezrael · Feb 13, 2017

You can use:

df = pd.DataFrame([A])
print (df)
   0  1  2    3 4
0  1  d  p  bab  

If all values are strings:

df = pd.DataFrame(np.array(A).reshape(-1,len(A)))
print (df)

   0  1  2    3 4
0  1  d  p  bab  

Thank you AKS:

df = pd.DataFrame(A).T
print (df)
   0  1  2    3 4
0  1  d  p  bab