python create a data frame with one row by a list

2020-07-03 08:03发布

问题:

in python, say I have a list [1,2,3,...,100], and I would like to use this list to create a dataframe which has one row and the row value is the list. What is the fastest and elegant way to do this?

回答1:

pass the list as a list param to data:

In [11]:

l = range(1,100)
pd.DataFrame(data=[l])
Out[11]:
   0   1   2   3   4   5   6   7   8   9  ...  89  90  91  92  93  94  95  96  \
0   1   2   3   4   5   6   7   8   9  10 ...  90  91  92  93  94  95  96  97   

   97  98  
0  98  99  

[1 rows x 99 columns]

You can pass the columns names as an arg to the DataFrame constructor or assign directly:

pd.DataFrame(data=[l], columns = col_list)

or

df.columns = col_list