Search code examples
pandasdataframecsvdata-analysis

How to drop rows for those column elements which are list?


enter image description here

In the above image, the last column ('Location') is a list. I need to drop those rows for the following conditions:

if first item in the list 'location' is greater than 60.0, those rows are not needed for me

if i use:

for i in range(len(output)):
    trip_df = output.drop(output[ output['location'][i][0] > 60].index)

Error is : KeywordError: True

I cannot simply put output['location'][0] since it would denote only the first row alone, how can i do this with or without the for loop?

P.S : Sorry for the image, I don't know how to put the values in a table


Solution

  • Use str method to obtain the first item:

    df = pd.DataFrame({"Location":[[61,50],[117,24],[3,5]],     #dummy data
                       "Values":[2,4,6]})
    
    print (df[~df["Location"].str[0].gt(60)])
    
      Location  Values
    2   [3, 5]       6