1

I have a dataframe where one of my columns includes rows of lists.

Each individual row in my column is a list of elements.

I want to create ONE list that includes all the values from each row.

I have tried

final_list = list(itertools.chain.from_iterable(mylist)) 

but this keeps each row has a list.

The total length of all of my rows is 1981. When I check for final_list it still has a length of 1981 which is wrong because each row has multiple elements in the lists.

I expect for the length of my final_list to have each row's list elements.

Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33
  • 4
    can you create a [mcve] for the same please? It makes it easier for people to help you. Also take a look at [this question](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) for some guidelines. – Paritosh Singh Jul 19 '19 at 14:05
  • Possible duplicate of [How to make a flat list out of list of lists](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) – a_guest Jul 19 '19 at 14:21

1 Answers1

0

Assuming you are using pandas.DataFrame (marked as df in code), you can work it out using pandas.DataFrame.values.tolist():

final_list = [item for sublist in df.values.tolist() for item in sublist]

Methodology of this list operation is described here.

errno98
  • 312
  • 2
  • 12