Have a dataframe looks like this:
TripId time_LAT_LON
0 [['2017-07-22 07:49:01',43.96529, -76.50005], ['2017-07-22 07:50:01',43.96046, -76.50513],....
how can I extract LAT and LON values from time_LAT_LON column and save them into a new dataframe for further calculations?
I don't want to modify original dataframe so i think of creating a new dataframe :
new_df=df.copy(deep=true)
for i , sublst in enumerate(new_df['time_LAT_LON']):
del(sublst[0])
But the time values in original dataframe is deleted too.
by the way , I don't want to iterate through original dataframe and actually want LAT and LON values in new independent dataframe
CodePudding user response:
You can try Series.apply
out = df['time_LAT_LON'].apply(lambda lsts: [lst[1:] for lst in lsts]).to_frame('LAT_LON')
print(out)
LAT_LON
0 [[43.96529, -76.50005], [43.96046, -76.50513]]
Or you can try explode if you want output to separated column
out = pd.DataFrame(df['time_LAT_LON'].explode().tolist(), columns='time_LAT_LON'.split('_')).drop(columns='time')
# or
out = df['time_LAT_LON'].explode().apply(pd.Series).set_axis('time_LAT_LON'.split('_'), axis=1).drop(columns='time')
print(out)
LAT LON
0 43.96529 -76.50005
0 43.96046 -76.50513