Home > other >  How can I drop multiple rows based on a list of values
How can I drop multiple rows based on a list of values

Time:10-27

So i have a list of values of longitude and latitude

[[-82.99194508, 40.04649963], [-82.99223727, 40.04659349], [-82.99198391, 40.04614863], [-82.99211513, 40.04627218]]

and a dataframe

hhinc8,owncar,longitude,latitude
5,1,-82.99194508,40.04649963
6,2,-82.99584706,40.03738548
5,1,-82.99697268,40.02674247
6,2,-82.99160441,40.03612997
6,2,-82.994716,40.04691778
6,2,-82.99793728,40.04385713
5,2,-82.98986012,40.03789279
7,2,-82.99602685,40.03216463
6,1,-82.99362168,40.0372023
.
.
.

How can I drop all the rows from the dataframe that have same longitude and latitude values as the given list

CodePudding user response:

Here is one way to do it

#create a DF from the list of the long/lat you want to exclude

df2=pd.DataFrame(l, columns=['longitude1','latitude1'])

# do a left merge
# choose rows that has any null value

out=df[df.merge(df2, 
                how='left', 
                left_on=['longitude','latitude'],
                right_on=['longitude1','latitude1']).isna().any(axis=1)]

out

OR just in a single statement

out=df[df.merge(pd.DataFrame(l, columns=['longitude1','latitude1']), 
                how='left',
                left_on=['longitude','latitude'],
                right_on=['longitude1','latitude1'])
       .isna()
       .any(axis=1)]
out
    hhinc8  owncar  longitude   latitude
1        6       2  -82.995847  40.037385
2        5       1  -82.996973  40.026742
3        6       2  -82.991604  40.036130
4        6       2  -82.994716  40.046918
5        6       2  -82.997937  40.043857
6        5       2  -82.989860  40.037893
7        7       2  -82.996027  40.032165
8        6       1  -82.993622  40.037202

CodePudding user response:

Here's another approach to drop by using for loops:

# Assuming you already have the code to create a dataframe 'df'
for i in ls: # Loop through the list you told
    for index, rows in df.iterrows(): # Loop through the rows
        # Check if the longitude and latitude are equal your list
        if [rows.longitude, rows.latitude] == i: 
            df = df.drop(index) # Drop it
df
  • Related