Python: create a pandas data frame from a list Python: create a pandas data frame from a list python-3.x python-3.x

Python: create a pandas data frame from a list


DataFrame.from_records treats string as a character list. so it needs as many columns as length of string.

You could simply use the DataFrame constructor.

In [3]: pd.DataFrame(q_list, columns=['q_data'])Out[3]:      q_data0  1123544011  1161155262  1149093123  1224254914  1319570255  111373473


In[20]: test_list = [['a','b','c'], ['AA','BB','CC']]In[21]: pd.DataFrame(test_list, columns=['col_A', 'col_B', 'col_C'])Out[21]:   col_A col_B col_C0     a     b     c1    AA    BB    CCIn[22]: pd.DataFrame(test_list, index=['col_low', 'col_up']).TOut[22]:   col_low col_up0       a     AA1       b     BB2       c     CC


If you want to create a DataFrame from multiple lists you can simply zip the lists. This returns a 'zip' object. So you convert back to a list.

mydf = pd.DataFrame(list(zip(lstA, lstB)), columns = ['My List A', 'My List B'])