Home > Mobile >  lookup field values in another field
lookup field values in another field

Time:07-06

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:

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

CodePudding user response:

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]
  • Related