Find row number in column where it matches any other value in column of other dataframe

vasili111 :

I have a code:

import pandas as pd
import numpy as np

arm_1_and_m1_df = pd.DataFrame({ 'record_id': [1, 4, 3, np.nan],
                   'two': [1, 2, np.nan , 4]
                 })

redcap_final_arm1_data = pd.DataFrame({ 'record_id': [1, 2, 3, 4, 5, 6, 7, 8, 9, np.nan],
                   'two': [1, 2, 3, 4, 5, 6, 7, 8, 9, np.nan]
                 })

ahk_ids_new=[]
for items in arm_1_and_m1_df['record_id'].iteritems():     # https://www.geeksforgeeks.org/python-pandas-series-iteritems/
    ahk_ids_new.append(np.where(redcap_final_arm1_data['record_id'] == items))    # https://stackoverflow.com/questions/48519062/rs-which-and-which-min-equivalent-in-python

After running code above and after ahk_ids_new the content of ahk_ids_new is:

[(array([], dtype=int64),),
 (array([], dtype=int64),),
 (array([], dtype=int64),),
 (array([], dtype=int64),)]

Values in redcap_final_arm1_data['record_id'] are unique.

Question: I want to get all row numbers (index) of redcap_final_arm1_data['record_id'] in ahk_ids_new where redcap_final_arm1_data['record_id'] has the same value as any values in arm_1_and_m1_df['record_id']. How to do that?

Expected output (content) of ahk_ids_new:

Out[57]: [0, 3, 2, 9]

If there is a better way to do what I need with data frames from my code please post your better variant instead of fixing my code.

Andy L. :

Try isin and slicing on index

a_index = (redcap_final_arm1_data.index[redcap_final_arm1_data.record_id
                                           .isin(arm_1_and_m1_df.record_id)].tolist())

Out[1355]: [0, 2, 3, 9]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23692&siteId=1