pandas 如何将带有列表值的熊猫列连接到一个列表中?

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

How to concatenate pandas column with list values into one list?

pythonlistpandas

提问by SarwatFatimaM

I have a dataframe with one of its column having a list at each index. I want to concatenate these lists into one list. I am using

我有一个数据框,其中一列在每个索引处都有一个列表。我想将这些列表连接成一个列表。我在用

ids = df.loc[0:index, 'User IDs'].values.tolist()

However, this results in ['[1,2,3,4......]']which is a string. Somehow each value in my list column is type str. I have tried converting using list(), literal_eval()but it does not work. The list()converts each element within a list into a string e.g. from [12,13,14...]to ['['1'',','2',','1',',','3'......]'].

然而,这导致 ['[1,2,3,4......]']which 是一个字符串。不知何故,我的列表列中的每个值都是 type str。我曾尝试使用 转换list()literal_eval()但它不起作用。该list()列表中的每个元素从转换成字符串如[12,13,14...]['['1'',','2',','1',',','3'......]']

How to concatenate pandas column with list values into one list? Kindly help out, I am banging my head on it for several hours.

如何将带有列表值的Pandas列连接到一个列表中?请帮忙,我正在敲打它几个小时。

回答by piRSquared

consider the dataframe df

考虑数据框 df

df = pd.DataFrame(dict(col1=[[1, 2, 3]] * 2))
print(df)

        col1
0  [1, 2, 3]
1  [1, 2, 3]

pandassimplest answer

pandas最简单的答案

df.col1.sum()

[1, 2, 3, 1, 2, 3]

numpy.concatenate

numpy.concatenate

np.concatenate(df.col1)

array([1, 2, 3, 1, 2, 3])

chain

chain

from itertools import chain

list(chain(*df.col1))

[1, 2, 3, 1, 2, 3]


response to comments:
I think your columns are strings

回复评论:
我认为你的列是字符串

from ast import literal_eval

df.col1 = df.col1.apply(literal_eval)

If instead your column is string values that look like lists

如果您的列是看起来像列表的字符串值

df = pd.DataFrame(dict(col1=['[1, 2, 3]'] * 2))
print(df)  # will look the same

        col1
0  [1, 2, 3]
1  [1, 2, 3]

However pd.Series.sumdoes not work the same.

但是pd.Series.sum不一样。

df.col1.sum()

'[1, 2, 3][1, 2, 3]'

We need to evaluate the strings as if they are literals and then sum

我们需要评估字符串,就好像它们是文字一样,然后 sum

df.col1.apply(literal_eval).sum()

[1, 2, 3, 1, 2, 3]

回答by zipa

If you want to flatten the list this is pythonicway to do it:

如果您想展平列表,可以pythonic这样做:

import pandas as pd

将Pandas导入为 pd

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

a = df['A'].tolist()
a = [i for j in a for i in j]
print a