python - check if pair of values is in pair of columns in pandas -


basically, have latitude , longitude (on grid) in 2 different columns. getting fed two-element lists (could numpy arrays) of new coordinate set , want check if duplicate before add it.

for example, data:

df = pd.dataframe([[4,8, 'wolf', 'predator', 10],               [5,6,'cow', 'prey', 10],               [8, 2, 'rabbit', 'prey', 10],               [5, 3, 'rabbit', 'prey', 10],               [3, 2, 'cow', 'prey', 10],               [7, 5, 'rabbit', 'prey', 10]],               columns = ['lat', 'long', 'name', 'kingdom', 'energy'])  newcoords1 = [4,4] newcoords2 = [7,5] 

is possible write 1 if statement tell me whether there row latitude , longitude. in pseudo code:

if newcoords1 in df['lat', 'long']:     print('yes! ' + str(newcoords1)) 

(in example, newcoords1 should false , newcoords2 should true.

sidenote: (newcoords1[0] in df['lat']) & (newcoords1[1] in df['long']) doesn't work because checks them independently, need know if combination appears in single row.

thank in advance!

you can way:

in [140]: df.query('@newcoords2[0] == lat , @newcoords2[1] == long') out[140]:    lat  long    name kingdom  energy 5    7     5  rabbit    prey      10  in [146]: df.query('@newcoords2[0] == lat , @newcoords2[1] == long').empty out[146]: false 

the following line return number of found rows:

in [147]: df.query('@newcoords2[0] == lat , @newcoords2[1] == long').shape[0] out[147]: 1 

or using numpy approach:

in [103]: df[(df[['lat','long']].values == newcoords2).all(axis=1)] out[103]:    lat  long    name kingdom  energy 5    7     5  rabbit    prey      10 

this show whether @ least 1 row has been found:

in [113]: (df[['lat','long']].values == newcoords2).all(axis=1).any() out[113]: true  in [114]: (df[['lat','long']].values == newcoords1).all(axis=1).any() out[114]: false 

explanation:

in [104]: df[['lat','long']].values == newcoords2 out[104]: array([[false, false],        [false, false],        [false, false],        [false, false],        [false, false],        [ true,  true]], dtype=bool)  in [105]: (df[['lat','long']].values == newcoords2).all(axis=1) out[105]: array([false, false, false, false, false,  true], dtype=bool) 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -