Python 将列表转换为熊猫数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42593104/
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-08-19 21:56:03  来源:igfitidea点击:

Convert list into a pandas data frame

pythonpandas

提问by Kay

I am trying to convert my output into a pandas data frame and I am struggling. I have this list

我正在尝试将我的输出转换为 Pandas 数据框,但我正在挣扎。我有这个清单

my_list = [1,2,3,4,5,6,7,8,9]

I want to create a pandas data frame that would have 3 columns and three rows. I try using

我想创建一个有 3 列和 3 行的 Pandas 数据框。我尝试使用

df = pd.DataFrame(my_list, columns = list("abc"))

but it doesn't seem to be working for me. Any help would be appreciated.

但它似乎对我不起作用。任何帮助,将不胜感激。

回答by jezrael

You need convert listto numpy arrayand then reshape:

您需要转换listnumpy array然后reshape

df = pd.DataFrame(np.array(my_list).reshape(3,3), columns = list("abc"))
print (df)
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9