Skip to main content
 首页 » 编程设计

python之类型错误 : '' not supported between instances of 'int' and 'list' in numpy intersect1d

2024年09月07日37java哥

我想在两个列表 phone 和 phone_office 之间应用交集。所以我写了下面的代码

phone=df_final_naFill.iloc[0,4] 
type(phone) # List 
s=pd.Series(phone) 
type(s) #pandas.core.series.Series 
a=pd.Series(s.apply(pd.Series).stack().astype(int).groupby(level=0).apply(list)) 
phone_office=df_final_naFill.iloc[0,6] 
# type(phone_office) #List 
h=pd.Series(phone_office) 
phone_comb=np.intersect1d(a,h) 

但是在运行代码之后,我得到了以下错误信息

  File "<ipython-input-206-3512341621de>", line 1, in <module> 
    phone_comb=np.intersect1d(a,h) 
 
  File "C:\ProgramData\Anaconda3\lib\site-packages\numpy\lib\arraysetops.py", line 337, in intersect1d 
    aux.sort() 
 
TypeError: '<' not supported between instances of 'int' and 'list' 

前5排手机的样子

0    [735015372, 72151508105, 7217511580, 721150431... 
1                                                   [] 
2    [735152771, 7351515043, 7115380870, 7115427... 
3    [7111332015, 73140214, 737443075, 7110815115... 
4    [718218718, 718221342, 73551401, 71811507... 

我的h看起来像

      1541291 
      1011248 
      1015925 
      1013535 
      1093131 
      1125310 
      1154955 
      1158590 
      1103552 
      1121881 

看起来像

[92972897] 
[92020181] 
[92038083] 
[92083771] 
[92611383] 
[93290963] 
[93262309] 
[92966917] 
[93181110] 
[93396990] 
[93186911] 
[92011329] 

尽管 a 和 h 的类型显示为 pandas.core.series.Series。我猜 a 的这种格式会在一些迭代中出现。大多数时候我的代码运行时没有任何错误。

你能建议我在 python 3.x 中需要做哪些更改吗?

请您参考如下方法:

如果两个 list 都没有嵌套,你可以大大简化你的代码:

phone_comb=np.intersect1d(phone, phone_office) 

如果需要比较原始DataFrame中的每一行:

f = lambda x: np.intersect1d(x.iat[4], x.iat[6]) 
df_final_naFill['inter'] = df_final_naFill.apply(f, axis=1).values.tolist()