I need to compare items in a list to a column in df1 and create new column in df2 with value from a different column on the same row in df1, if there is a match between a list item and the compared df1 column
My code and results
import pandas as pd
# initialize list elements
new_data = [[10,'a'], [20, 'b'], [30, 'c'], [40, 'd']]
old_data = [['10a','A', 10.05], [22, 'B', 12.10], [40, 'C', 1.10],
[20, 'D', 8.05], [15, 'E', 9.00]]
lst = [20, 40, 50, 70, '20a']
# Create the pandas DataFrame with column name is provided explicitly
df1 = pd.DataFrame(new_data, columns=['Numbers', 'Letters'])
print(df)
df2 = pd.DataFrame(old_data, columns=['level', 'cap', 'time'])
df2['Exit'] = df1.apply(lambda x: x['Letters'] if \
str(x['Numbers']) in lst else 0, axis=1)
print(df2)
Result
level cap time Exit
0 10a A 10.05 0.0
1 22 B 12.10 0.0
2 40 C 1.10 0.0
3 20 D 8.05 0.0
4 15 E 9.00 NaN
But Expected
level cap time Exit
0 10a A 10.05 0
1 22 B 12.10 0
2 40 C 1.10 d
3 20 D 8.05 b
4 15 E 9.00 0
I am missing something?