0

I have a dataframe of five columns where three of these columns contain lists of multiple values, which are ("Long", "Lat", Time_stamp"). Note that all ("Long", "Lat", Time_stamp") columns as object data-type. Original dataframe Rather than storing multiple values in a cell, I'd like to split these values of the three columns into new three columns with including each lists element, so that each item in these lists gets its own rows (with the same values in all other columns( index and car_id). So the output I am looking for is as follows :

index car_id Long Lat Time_stamp
0 8919 108.99553 34.27859 1539041301
0 8919 108.99552 34.27822 1539041304
0 8919 108.99552 34.27786 1539041307
0 8919 108.99552 34.27748 1539041310
1 19785 108.9665 34.20515 1539014039
1 19785 108.9665 34.20543 1539014042
1 19785 108.9665 34.20572 1539014046
1 19785 108.9665 34.20602 1539014049

And so on.....

Thank you for your help....

M_Fatih89
  • 49
  • 8

2 Answers2

0

Assumption

The following code assumes only a fixed number of columns need to be normalized.

Implementation

import pandas as pd

df = pd.DataFrame({'car_id': [[1], [2]], 'longitudes': [[108.99553, 108.99552], [108.9665]], 'latitudes': [[34.27859, 34.27822, 34.27786], [34.20543, 34.20572]]})

print(df)

pd.DataFrame([car_id, longitude, latitude] for car_id, longitudes, latitudes in df.values for car_id in car_id for longitude in longitudes for latitude in latitudes)
Thiyanesh
  • 2,360
  • 1
  • 4
  • 11
0

If the values in a column are in a list you can use explode to distribute each of the values of the list into its own row.

please try

              df2.explode('Long').explode('Lat').explode('Time_stamp')
theDBA
  • 239
  • 1
  • 5