Skip to content Skip to sidebar Skip to footer

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(

Solution 1:

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]

Solution 2:

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

import pandas as 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

Post a Comment for "How To Concatenate Pandas Column With List Values Into One List?"