Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

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

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'......]'].

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

consider the dataframe df

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

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

pandas simplest answer

df.col1.sum()

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

numpy.concatenate

np.concatenate(df.col1)

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

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.sum does not work the same.

df.col1.sum()

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

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...