pandas python通过列表创建一个带有一行的数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29079408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:03:18  来源:igfitidea点击:

python create a data frame with one row by a list

pythonlistpandasdataframe

提问by user2854008

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?

在 python 中,假设我有一个列表 [1,2,3,...,100],我想使用这个列表来创建一个数据框,它有一行,行值是列表。这样做的最快和优雅的方法是什么?

回答by EdChum

pass the list as a list param to data:

将列表作为列表参数传递给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 DataFrameconstructor or assign directly:

您可以将列名称作为参数传递给DataFrame构造函数或直接分配:

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

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

or

或者

df.columns = col_list