Search code examples
pythonpandasdataframenumpygeopy

Slicing a list item that exists in a pandas dataframe


I have a list (named split_dfs) that contains several dataframes with 100 rows in each. These dataframes each contain columns with coordinate data that is stored in lists. i.e there is a column named Destination Coordinates and every row has data such as [41.67907634659514, -73.88281655538246, 0.0]. They look as follows:

enter image description here

I am attempting to compute the mile difference between the two coordinate columns, but as evident, one of the coordinate columns has 3 points(the 0 is for altitude) which causes an error in the computation. To reiterate, each row in the coordinate columns contains a list.

How can I loop through every dataframe in the list and slice the Destination Coordinate column to get rid of the 3rd point and keep only the first 2 in each row. I have tried the following:

for i in split_dfs:
    for x in i["Destination Coordinates"]:
        x=x[:2]

This returns 'NoneType object is not subscriptable'. The transformation I am trying to perform works if I do it in a list comprehension for every dataframe individually i.e:

first_group["Destination Coordinates"]=[i[:2] for i in first_group["Destination Coordinates"]]

I would like to be able to do this in a loop rather than write code for every dataframe individually.

Thank you.


Solution

  • You cannot change an element of a dataframe that way.

    What you want is probably:

    for i in split_dfs:
        i['Destination Coordinates'] = i['Destination Coordinates'].transform(
            lambda x: x[:2] if isinstance(x, list) else x)