0

Let's say there is a data frame that has a column with list values, ie.:

df = pd.DataFrame([
    [1, ['a'], 2],
    [3, ['b'], 4],
    [5, ['b','c'], 6],
    [7, ['c'], 8],
    [9, ['a','c'], 10]
])

How can I expand this dataframe, so that each row is copied for every element in the list with only a single value in the middle column? The resulted data frame should have 3 columns and look like this:

pd.DataFrame([
    [1, 'a', 2],
    [3, 'b', 4],
    [5, 'b', 6],
    [5, 'c', 6],
    [7, 'c', 8],
    [9, 'a', 10],
    [9, 'c', 10]
])

I am looking for a pandorable solution using apply() or such, otherwise I would know how to iterate over rows and create a new dataframe according the content of the middle cell.

mac13k
  • 2,423
  • 23
  • 34

0 Answers0