nerd
nerd

Reputation: 493

Combine multiple rows of lists into one big list using pandas

I have a pandas data frame with multiple lists. Is there any way to combine all of the lists from all rows ( there could be 100 rows ) into one big list?

     fruits
0    [apple, banana, orange]
1    [berry, lemon]
2    [apple, tomato]
3    [lime, orange, banana]

Expected Output

[ 'apple', 'banana', 'orange', 'berry', 'lemon', 'apple', 'tomato', 'lime', 'orange', 'banana' ] 

Upvotes: 1

Views: 2899

Answers (3)

Mark Wang
Mark Wang

Reputation: 2757

Try:

df["fruits"].sum()

Upvotes: 4

deadshot
deadshot

Reputation: 9061

use df.explode()

lst = df['fruits'].explode().to_list()

Upvotes: 7

Hadi Rahjoo
Hadi Rahjoo

Reputation: 185

Try extend :

result = []
for row in fruits :
    result.extend(row)

Upvotes: 2

Related Questions