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.

  • you want something like `pdf.apply(lambda x: x['City'][2] == x['UF'][0], axis=1)`? – Tarifazo Feb 27 '19 at 15:13
  • I think you would be better off storing the lists in three [different columns](https://stackoverflow.com/questions/35491274/pandas-split-column-of-lists-into-multiple-columns) and then comparing the relevant columns. – FChm Feb 27 '19 at 17:05

1 Answers1

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!