I have code that loops through a column of a dataframe. How can I make this code faster? (Python/Pandas) I have datasetA with 90,000 rows and datasetB with 5,000 rows. Each dataset has a column called &quot;ID&quot; with employee IDs. My goal is to to create another column in datasetA that identifies whether the employee ID in datasetA is also in datasetB with a True/False. Additionally, there are most likely some multiples for certain employees/employee ids in both datasets. I am fairly certain that the code I wrote works, but it is way too slow, and I was wondering what I could change to make it faster? Thanks!
#Creating the new column to identify whether the ID in datasetA is also in datasetB.

datasetA[&quot;inB&quot;] = &quot;Empty&quot;

# Looping through

for id_num in datasetA[&quot;ID&quot;]:
    filt = (datasetA[&quot;ID&quot;] == id_num)
    if (datasetB[&quot;ID&quot;] == id_num).any():
        datasetA.loc[filt, &quot;inB&quot;] = True
    else:
        datasetA.loc[filt, &quot;inB&quot;] = False
```
Is this what you want?
import pandas as pd

datasetA = pd.DataFrame(
    [
        [
            'ID222'
        ],
        [
            'ID233'
        ],
        [
            'ID2123'
        ],
        [
            'ID233'
        ]
    ], columns = ['ID']
)

datasetB = pd.DataFrame(
    [
        [
            'ID222'
        ],
        [
            'ID233'
        ],
        [
            'ID212355'
        ],
        [
            'ID233'
        ]
    ], columns = ['ID']
)
datasetA[&quot;inB&quot;] = datasetA.ID.isin(datasetB.ID)
datasetA.drop_duplicates()

    ID  inB
0   ID222   True
1   ID233   True
2   ID2123  False