I want to lookup column in dataframe (Lookup item) using pandas (or anything else). If the value is found in that column then corresponding value in another column (Corresponding item) of same dataframe is picked up with both recorded into another dataframe.
Example cols are:
- lookup_id = [111, 222, 333, 444, 777 , 1089 , 3562 ]
- id_number = [111, 23, 333, 444, 10101 ,777 , 222 ]
items = arr.array('i', [111, 222, 333, 444, 777 , 1089 , 3562 ])
new_df = []
for item in items:
lookup_id = item
id_number = get_score_by_id(df_past, lookup_id)
if id_number is not None:
df_append = (f'{lookup_id}, {id_number}')
else:
df_append = ('NAN')
print(df_append)
The required output dataframe is shown below:
| Lookup item | Corresponding item |
|---|---|
| 111 | OK |
| 222 | OK |
| 333 | OK |
| 444 | OK |
| 777 | OK |
| 1089 | NAN |
| 3562 | NAN |
So here all are found so 'OK' returned and 1089 and 3562 do not exist so NAN recored.
The script has been developed to replace an xls VLOOKUP so it's a vlookup/append but cant get the append to add new fields in next row fresh df.
I can get it to work just printing the output but want the new_df populated and that’s primarily intent of question.
Thanks.
get_score_by_idordf_past. Also, please include the exact expected output based on a provided sample ofdf_past.new_df.append( df_append )? And late you can put this list as another column inDataFramepandasthen create code with DataFrame - likedf = pd.DataFrame({'lookup_id': [...]})OKNANthen first you can get it as True/False withdf['result'] = df['lookup_id'].isin(df_past['id_number'])and later you can replace True/False with stringsdf['result'] = np.where(df['result'], 'OK', 'NAN'). And if you need more details then you can trydf['lookup_id'].apply( my_function )to runmy_functionon every row - and thismy_functioncan search indf_past['id_number']and getID