Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

lookup field values in another field

this df :

import pandas as pd
import numpy as np

Open_Time_s=['2022-04-30 11:05:00+03:00','2022-04-30 11:10:00+03:00']
intersect=[np.nan,np.nan,'intersect_2022-04-30 11:05:00+03:00','intersect_2022-04-30 11:10:00+03:00']

df3 = pd.DataFrame.from_dict({'Open Time':intersect,'intersect':intersect},orient='index').transpose()

I need to return match indexes.
For some unknown reason, the search is not working.
Maybe the reason is in the data format ?

code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

print(df3[df3['Open Time']==df3['intersect'].str.replace(r'intersect_', '')])

>Solution :

For what you want, just use .index.tolist() on the suggestion of @mozway.

Note also I am defining df3 to include Open_Time_s.

>>> Open_Time_s=['2022-04-30 11:05:00+03:00','2022-04-30 11:10:00+03:00']
>>> intersect=[np.nan,np.nan,'intersect_2022-04-30 11:05:00+03:00','intersect_2022-04-30 11:10:00+03:00']

>>> df3 = pd.DataFrame.from_dict({'Open Time':Open_Time_s,'intersect':intersect},orient='index').transpose()
>>> df3
                   Open Time                            intersect
0  2022-04-30 11:05:00+03:00                                  NaN
1  2022-04-30 11:10:00+03:00                                  NaN
2                       None  intersect_2022-04-30 11:05:00+03:00
3                       None  intersect_2022-04-30 11:10:00+03:00

>>> df3[df3['Open Time'].isin(df3['intersect'].str.replace(r'intersect_', ''))].index.tolist()
[0, 1]

With slightly different data

>>> Open_Time_s=['2022-04-17 11:05:00+03:00','2022-04-30 11:05:00+03:00','2022-04-30 11:10:00+03:00']
>>> df3 = pd.DataFrame.from_dict({'Open Time':Open_Time_s,'intersect':intersect},orient='index').transpose()
>>> df3[df3['Open Time'].isin(df3['intersect'].str.replace(r'intersect_', ''))].index.tolist()
[1, 2]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading