I have a dataframe, df
, of salary data:
State,Annual Salary
New York, 132826
New Hampshire,128704
California,127388
Vermont,121599
Idaho,120011
And a function, get_taxes_from_api
that calls an API and returns tax numbers for an input State and Annual Salary as a data frame with only 1 row like this:
State,annual.fica.amount,annual.federal.amount,annual.state.amount
North Carolina,8918,40334,6364
it is of type: <class 'pandas.core.frame.DataFrame'>
not series.
I want to call the API on each row of df and then merge each of the resulting 1 row data frames. Something like:
State,Annual Salary,annual.fica.amount,annual.federal.amount,annual.state.amount
North Carolina, 116500,8918,40334,6364
New York, 132826, . . .
New Hampshire,128704, . . .
California,127388, . . .
Vermont,121599, . . .
Idaho,120011, . . .
How do I do this? I ran into an error that my lambda below created a Series instead of a dataframe so went down this rabbit hole of using result_type=expand like here 'https://stackoverflow.com/a/62849468/2415706' but it is still broken:
all_tax_df = df[['State','Annual Salary']].apply(lambda row: get_taxes_from_api(row['State'],row['Annual Salary']), axis=1, result_type='expand')
# merge all_tax_df with df on 'State'