2

I have a dataframe where one of its columns is a list. I would like to extract every time one value from that list and append a new row based on that to a new dataframe.

df:

       0      1      2
0   'Abcd'   5623   ['one', 'two']
1   'Brdd'   5624   ['three']
2   'Vbcd'   4223   ['five', 'six', 'seven']
3   'Mkln'   5873   []

Results:

       0      1      2
0   'Abcd'   5623   'one'
1   'Abcd'   5623   'two'
2   'Brdd'   5624   'three'
3   'Vbcd'   4223   'five'
4   'Vbcd'   4223   'six'
5   'Vbcd'   4223   'seven'

I came up with the below function but it is super slow. I wonder if there is a better way to do this in pandas.

for index, row in df.iterrows():
    for el in df['Column']:
        temp = df.iloc[index]
        temp['Column'] = el
        df_clear = df_clear.append(temp)
    print("Currently on row: {}; Currently iterated {}% of rows".format(index, (index + 1) / len(df.index) * 100))
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NouName
  • 87
  • 5

0 Answers0