0

I've been looking someway to compare an specific array item using numpy.where. I have a pandas Dataframe as follow:

   Id   City                    UF
0   1   [5057, Itu, 26]         [26, São Paulo]
1   2   [5366, Sorocaba, 26]    None
2   3   [5347, São Paulo, 26]   [26, São Paulo]
3   4   [3288, Curitiba, 18]    [18, Paraná]
4   5   [162, Manaus, 3]        [3, Amazonas]

I want to compare, for each line, the third item from array City with first item from array UF. I've tried like this:

np.where((pdf.loc[pdf['Cidade']:, 2] == pdf.loc[pdf['UF']:, 0]), True, False)

But had no success. Every similar sample i've found is about comparing the entire array.

here are some links i'd checked here also here

Thanks in advice.

2
  • you want something like pdf.apply(lambda x: x['City'][2] == x['UF'][0], axis=1)? Commented Feb 27, 2019 at 15:13
  • I think you would be better off storing the lists in three different columns and then comparing the relevant columns. Commented Feb 27, 2019 at 17:05

1 Answer 1

0

Almost what you've said @Mstaino.

I'd got what i wanted using the code below:

np.where((pdf['Cidade'].apply(lambda x: x[2] if x is not None else None) == pdf['UF'].apply(lambda x: x[0] if x is not None else None)), True, False)

Thanks by the tips!

Sign up to request clarification or add additional context in comments.

Comments

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.