How to convert list to row dataframe with Pandas How to convert list to row dataframe with Pandas python python

How to convert list to row dataframe with Pandas


You can use:

df = pd.DataFrame([A])print (df)   0  1  2    3 40  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 40  1  d  p  bab  

Thank you AKS:

df = pd.DataFrame(A).Tprint (df)   0  1  2    3 40  1  d  p  bab  


This is somewhat peripheral to your particular issue, but I figured I would post it in case it helps someone else out.

To convert a list of lists (and give each column a name), just pass the list to the data attribute (along with your desired column names) when instantiating the new dataframe, like so:

my_python_list = [['foo1', 'bar1'],                  ['foo2', 'bar2']]new_df = pd.DataFrame(columns=['my_column_name_1', 'my_column_name_2'], data=my_python_list)

result:

  my_column_name_1 my_column_name_20             foo1             bar11             foo2             bar2