基本上,我在兩個不同的列中有經度和緯度(在網格上)。我得到了一個新座標集的雙元素列表(可能是numpy數組),我想在添加它之前檢查它是否是重複的。檢查一組值是否在熊貓中的一對列中
例如,我的數據:
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]
是否可以寫一個if
語句來告訴我,是否已經存在與緯度和經度一行。在僞代碼:
if newcoords1 in df['lat', 'long']:
print('yes! ' + str(newcoords1))
(在這個例子中,newcoords1
應false
和newcoords2
應true
旁註:(newcoords1[0] in df['lat']) & (newcoords1[1] in df['long'])
不起作用,因爲獨立檢查他們,但我需要知道這組合出現在單行
預先感謝您