pandas 将列表列表插入到pandas df的单列中

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

Insert list of lists into single column of pandas df

pythonpandas

提问by user3155053

I am trying to place multiple lists into a single column of a Pandas df. My list of lists is very long, so I cannot do so manually.

我正在尝试将多个列表放入 Pandas df 的单个列中。我的列表很长,所以我无法手动操作。

The desired out put would look like this:

所需的输出如下所示:

list_of_lists = [[1,2,3],[3,4,5],[5,6,7],...]
df = pd.DataFrame(list_of_lists)
>>> df
    0
0   [1,2,3]
1   [3,4,5]
2   [5,6,7]
3   ...

Thank you for the assistance.

感谢您的帮助。

采纳答案by Julien Spronck

What about

关于什么

df = pd.DataFrame({0: [[1,2,3],[3,4,5],[5,6,7]]})

回答by EdChum

You can assign it by wrapping it in a Seriesvector if you're trying to add to an existing df:

Series如果您尝试将其添加到现有 ,则可以通过将其包装在向量中来分配它df

In [7]:
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))
df

Out[7]:
          a         b         c
0 -1.675422 -0.696623 -1.025674
1  0.032192  0.582190  0.214029
2 -0.134230  0.991172 -0.177654
3 -1.688784  1.275275  0.029581
4 -0.528649  0.858710 -0.244512

In [9]:
df['new_col'] = pd.Series([[1,2,3],[3,4,5],[5,6,7]])
df

Out[9]:
          a         b         c    new_col
0 -1.675422 -0.696623 -1.025674  [1, 2, 3]
1  0.032192  0.582190  0.214029  [3, 4, 5]
2 -0.134230  0.991172 -0.177654  [5, 6, 7]
3 -1.688784  1.275275  0.029581        NaN
4 -0.528649  0.858710 -0.244512        NaN