I have two dataframes. The first dataframe is df_states and the second dataframe is state_lookup.
df_states
state code score
0 Texas 0 0.753549
1 Pennsylvania 0 0.998119
2 California 1 0.125751
3 Texas 2 0.125751
state_lookup
state code_0 code_1 code_2
0 Texas 2014 2015 2019
1 Pennsylvania 2015 2016 207
2 California 2014 2015 2019
I want to create a new column in df_states called 'year' which is based off the 'code' column which is based off the state_lookup table. So for example, if Texas has a code = 0 then based off the state_lookup df the year should be 2014. If Texas has a code = 2, then the year should be 2019.
This is what the end result should look like:
df_states
state code score year
0 Texas 0 0.753 2014
1 Pennsylvania 0 0.998 2015
2 California 1 0.125 2015
3 Texas 2 0.124 2019
I've tried using a for loop to iterate through each row, but am unable to get it to work. How would you achieve this?