gpt4 book ai didi

python - 检查两个 numpy 数组之间是否相等

转载 作者:行者123 更新时间:2023-11-30 21:51:19 26 4
gpt4 key购买 nike

我希望检查一个数据帧列中的类别是否与另一列中的类别匹配,即拼写等方面没有不匹配。

我现在有两个数组,代表感兴趣的列中的所有唯一值,并且我想返回第一个较小数组中但不在第二个较大数组中的任何值,因此我可以缩小我可能需要调整/重新拼写等的类别。我相信我应该使用 for 循环来评估每个数组,但我正在努力实现。下面是示例代码,谢谢:

borough_pm25 = pm25['Borough_x'].unique()
borough_pm25
array(['Barnet', 'Camden', 'Wandsworth', 'Hounslow', 'Southwark',
'Westminster', 'Kensington & Chelsea', 'Tower Hamlets',
'Islington', 'Kingston', 'Barking & Dagenham', 'Waltham Forest',
'Haringey', 'Lambeth', 'Enfield', 'Greenwich', 'Redbridge',
'Newham', 'City of London', 'Hackney', 'Richmond', 'Ealing',
'Hammersmith & Fulham', 'Lewisham', 'Sutton', 'Havering', 'Bexley',
'Bromley'], dtype=object)

borough_map = map_df['NAME'].unique()
borough_map
array(['Kingston upon Thames', 'Croydon', 'Bromley', 'Hounslow', 'Ealing',
'Havering', 'Hillingdon', 'Harrow', 'Brent', 'Barnet', 'Lambeth',
'Southwark', 'Lewisham', 'Greenwich', 'Bexley', 'Enfield',
'Waltham Forest', 'Redbridge', 'Sutton', 'Richmond upon Thames',
'Merton', 'Wandsworth', 'Hammersmith and Fulham',
'Kensington and Chelsea', 'Westminster', 'Camden', 'Tower Hamlets',
'Islington', 'Hackney', 'Haringey', 'Newham',
'Barking and Dagenham', 'City of London'], dtype=object)

最佳答案

您可以使用set操作。

import numpy as np
a=np.array(['Barnet', 'Camden', 'Wandsworth', 'Hounslow', 'Southwark',
'Westminster', 'Kensington & Chelsea', 'Tower Hamlets',
'Islington', 'Kingston', 'Barking & Dagenham', 'Waltham Forest',
'Haringey', 'Lambeth', 'Enfield', 'Greenwich', 'Redbridge',
'Newham', 'City of London', 'Hackney', 'Richmond', 'Ealing',
'Hammersmith & Fulham', 'Lewisham', 'Sutton', 'Havering', 'Bexley',
'Bromley'])
b=np.array(['Kingston upon Thames', 'Croydon', 'Bromley', 'Hounslow', 'Ealing',
'Havering', 'Hillingdon', 'Harrow', 'Brent', 'Barnet', 'Lambeth',
'Southwark', 'Lewisham', 'Greenwich', 'Bexley', 'Enfield',
'Waltham Forest', 'Redbridge', 'Sutton', 'Richmond upon Thames',
'Merton', 'Wandsworth', 'Hammersmith and Fulham',
'Kensington and Chelsea', 'Westminster', 'Camden', 'Tower Hamlets',
'Islington', 'Hackney', 'Haringey', 'Newham',
'Barking and Dagenham', 'City of London'])

print(set(a)-set(b)) #(set A – set B) will be the elements present in set A but not in B
print(set(b)-set(a)) #(set B – set A) will be the elements present in set B but not in set A
print(set(a)-set(b)|set(b)-set(a))
<小时/>
{'Barking & Dagenham',
'Hammersmith & Fulham',
'Kensington & Chelsea',
'Kingston',
'Richmond'} #set(a)-set(b)

{'Barking and Dagenham',
'Brent',
'Croydon',
'Hammersmith and Fulham',
'Harrow',
'Hillingdon',
'Kensington and Chelsea',
'Kingston upon Thames',
'Merton',
'Richmond upon Thames'} #set(b)-set(a)

{'Barking & Dagenham',
'Barking and Dagenham',
'Brent',
'Croydon',
'Hammersmith & Fulham',
'Hammersmith and Fulham',
'Harrow',
'Hillingdon',
'Kensington & Chelsea',
'Kensington and Chelsea',
'Kingston',
'Kingston upon Thames',
'Merton',
'Richmond',
'Richmond upon Thames'}

关于python - 检查两个 numpy 数组之间是否相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60129509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com