Tanvi Mirza
Tanvi Mirza

Reputation: 863

TypeError: '<' not supported between instances of 'int' and 'list' in numpy intersect1d

I want to apply intersect between two list,phone & phone_office.So I wrote following code

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)

But after running the code, I'm getting following error message

  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'

The first 5 rows of phone looks like

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

my h looks like

      1541291
      1011248
      1015925
      1013535
      1093131
      1125310
      1154955
      1158590
      1103552
      1121881

and a looks like

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

although type of a & h are showing as pandas.core.series.Series. I'm guessing this type of format for a is coming in some iterations.Most of the time my code is running without any error.

Can you suggest me what changes I need to do in python 3.x?

Upvotes: 2

Views: 1367

Answers (1)

jezrael
jezrael

Reputation: 862851

If both lists are not nested you can simplify your code a lot:

phone_comb=np.intersect1d(phone, phone_office)

And if need compare each row in original 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()

Upvotes: 2

Related Questions