Skip to content Skip to sidebar Skip to footer

Transform Pandas Column Of Nested Lists

how do I get a pandas column of nested lists in the form: [['6.65539026 -1.24900830'], ['6.65537977 -1.24882162'], ['6.65537977 -1.24882162'], ['6.65544653 -1.24888170'], ['6.6

Solution 1:

If you've a nested list column like this:

df = pd.DataFrame({'col' : [[['6.65539026 -1.24900830'],
 ['6.65537977 -1.24882162'],
 ['6.65537977 -1.24882162'],
 ['6.65544653 -1.24888170'],
 ['6.65548515 -1.24828506'],
 ['6.65574646 -1.24843669'],
 ['6.65588522 -1.24853671'],
 ['6.65616179 -1.24875331'],
 ['6.65600824 -1.24891996'],
 ['6.65566158 -1.24909663'],
 ['6.65554523 -1.24906671']]]})

                                                                                                                                                                                                                                                                                              col
0[[6.65539026 -1.24900830], [6.65537977 -1.24882162], [6.65537977 -1.24882162], [6.65544653 -1.24888170], [6.65548515 -1.24828506], [6.65574646 -1.24843669], [6.65588522 -1.24853671], [6.65616179 -1.24875331], [6.65600824 -1.24891996], [6.65566158 -1.24909663], [6.65554523 -1.24906671]]

Then you can try:

k = df.col.explode().str[0].str.split(' ')
df['col'] = k.groupby(k.index).agg(list)

OUTPUT:

                                                                                                                                                                                 col
0[[6.65539026, -1.24900830], [6.65537977, -1.24882162], [6.65537977, -1.24882162], [6.65544653, -1.24888170], [6.65548515, -1.24828506], [6.65574646, -1.24843669], [6.65588522, -1.24853671], [6.65616179, -1.24875331], [6.65600824, -1.24891996], [6.65566158, -1.24909663], [6.65554523, -1.24906671]]

Post a Comment for "Transform Pandas Column Of Nested Lists"