I have one df with state rates(rates_gp). The other df has worker info (workers_df). I need to merge state rate to the worker so I can later compute the workers' comp rate for each employee.
Here is a sample:
I have one df with state rates(rates_gp). The other df has worker info (workers_df). I need to merge state rate to the worker so I can later compute the workers' comp rate for each employee.
Here is a sample:
Initialization of your data:
rates_gp = pd.DataFrame({'state': ['AL', 'AZ', 'CA', 'CO'], 'rate': [0.0046, 0.0033, 0.0036, 0.0053]})
rates_gp.set_index('state', inplace=True)
rates_gp
         rate
state        
AL     0.0046
AZ     0.0033
CA     0.0036
CO     0.0053
workers_df = pd.DataFrame({'Employeeid': [11, 12, 13, 14], 'state': ['AL', 'AL', 'AZ', 'AZ']
                           , 'salary': [2000, 3500, 1100, 4200]})
workers_df
    Employeeid  state   salary
0   11          AL      2000
1   12          AL      3500
2   13          AZ      1100
3   14          AZ      4200
And the solution would be:
merged_df = workers_df.merge(rates_gp, how='inner', on='state')
merged_df
    Employeeid  state   salary  rate
0   11          AL      2000    0.0046
1   12          AL      3500    0.0046
2   13          AZ      1100    0.0033
3   14          AZ      4200    0.0033
