2

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?

5
  • Could you show how a and h look like and what is the expected output? Commented Jan 19, 2018 at 22:36
  • I have given the base data from which a & h came.Expected outcome is intersection between these two.Any suggestions please? Commented Jan 20, 2018 at 5:07
  • Can any of you please help me to find the solution? Commented Jan 20, 2018 at 5:34
  • @tarashypka, added my a & h Commented Jan 20, 2018 at 6:14
  • Thanks @jezrael, it works. Yes my data is problematic.In some iterations it works with a=pd.Series(s.apply(pd.Series).stack().astype(int).groupby(l‌​evel=0).apply(list)) & some iteration it doesn't. Will your solution work if my a will have structure like h? Commented Jan 20, 2018 at 19:25

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

12 Comments

The more upvotes you get, the more envy, and so more downvotes... it becomes a problem when they downvote 0 answers so it actually looks very bad (-1).
@jezrael, Previously you made some changes in my a=pd.Series(s.apply(pd.Series).stack().astype(int).groupby(level=0).apply(list)) and added it in comment section.I'm not seeing that one. Can you please add it?
@jezrael,can you please suggest some alternative options of my a=pd.Series(s.apply(pd.Series).stack().astype(int).groupby(level=0).apply(list)). Actually my data is problematic.Using your phone_comb=np.intersect1d(phone, phone_office) I'm not getting the desired output.
Can you still share that code @jezrael which you think was wrong.I think it was working for me
@TanviMirza - there is problem I forget it ;) But data are confidental? Because if it does not working I think some data problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.