如何恢复保存在系列中的列表

How to recover lists that are saved in Series

我在 Series 中有列表,这些列表存储在 Dataframe 中。现在我想重新创建这些列表。我怎样才能做到这一点?我尝试用函数调用它们。例如:

    pd.Series([1, 2, 3], [4, 5, 6], ....)
    getList (df.ix[1,['column']])       

那么它现在如何再次返回一个包含多个元素的列表呢? tolist() 方法 returns 整个列表作为一个元素。

    In: pd.Series([[1, 2, 3]]).tolist()
    Out: [[1, 2, 3]] 

简而言之。我如何访问存储在系列中的列表的每个元素?

如果您想从 Pandas Series' then 'totlist() 中取回原始列表,应该没问题。如果这是你的意思,因为你的问题不是很清楚。 还有,你的pd.Series([1, 2, 3], [4, 5, 6], ....)不对,请看下面:

s = pd.Series([[1, 2, 3], [4, 5, 6], [7,8,9]])
s.tolist()
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

而且,如果你想得到原来的一个元素list

s[0]
[1, 2, 3]

s[0][0]
1

iloc[0] 为您提供系列的第一个元素,即列表。 [0] 获取该列表的第一个元素。

pd.Series([[1, 2, 3]]).iloc[0][0]

或:

pd.Series([[1, 2, 3]]).tolist()[0][0]

您还可以这样做:

pd.Series([[1, 2, 3]]).apply(lambda x: x[2])

获取所有第 3 个元素作为一个系列。

0    3
dtype: int64