I have df1 with column ‘col’ & df2 with columns ‘code’ and ‘id’. Column ‘col’ in df1 is mapped to ‘code’ column in df2. When I am running the below code:
def get_id_for_code(code):
try:
return df2.loc[df2['code'] == code, 'id'].iloc[0]
except IndexError:
return None
for index, row in df.iterrows():
result['resultId'] = get_id_for_code(row['col'])
The error is thrown as:
return df2.loc[df2['code'] == code, 'id'].iloc[0]
NameError: name 'df2' is not defined
Can someone please help with resolving this issue.
Note: The dataframes are generated dynamically. They are huge and internal to the company. A sample of the dataframe:
Sample DataFrames
import pandas as pd
df2 = pd.DataFrame({
'id': [1, 2, 3, 4],
'code': ['A1', 'B2', 'C3', 'D4']
})
df1 = pd.DataFrame({
'project': [1, 2, 3, 5, 7]
})
Thanks in advance.