合并从应用功能返回到数据框的数据范围?

发布于 2025-02-12 06:15:45 字数 1115 浏览 3 评论 0 原文

我的问题与这个问题有关:

这是我的代码版本:

col = ['State','Annual Salary']
dat = [['New York', 132826], ['New Hampshire',128704], ['California',127388], ['Vermont',121599], ['Idaho',120011]]
df = pd.DataFrame(dat, columns=col)

def get_taxes_from_api(state, annual_salary):
    return pd.DataFrame({'State': [state, state], 
                         'annual.fica.amount': [int(annual_salary * 0.067),
                                                int(annual_salary * 1.067)], 
                         'annual.federal.amount': [int(annual_salary * 0.3),
                                                   int(annual_salary * 1.3)], 
                         'annual.state.amount': [int(annual_salary * 0.048),
                                                 int(annual_salary * 1.048)]})

如何将get_taxes_from_api应用于每一行DF并将返回的数据范围合并到数据框架上?

唯一的区别是我的函数返回多行数据框,而不是1行数据框。因此,上述问题的解决方案对我的情况不起作用。 (而且我没有足够的声誉来在那里发表评论。)

My question is related to this question:

Merge dataframe with another dataframe created from apply function?

Here is my version of code:

col = ['State','Annual Salary']
dat = [['New York', 132826], ['New Hampshire',128704], ['California',127388], ['Vermont',121599], ['Idaho',120011]]
df = pd.DataFrame(dat, columns=col)

def get_taxes_from_api(state, annual_salary):
    return pd.DataFrame({'State': [state, state], 
                         'annual.fica.amount': [int(annual_salary * 0.067),
                                                int(annual_salary * 1.067)], 
                         'annual.federal.amount': [int(annual_salary * 0.3),
                                                   int(annual_salary * 1.3)], 
                         'annual.state.amount': [int(annual_salary * 0.048),
                                                 int(annual_salary * 1.048)]})

How do I apply get_taxes_from_api to each row of df and combine the returned dataframes into on dataframe?

The only difference is that my function returns a multiple-row dataframe, not a 1-row dataframe. So the solution to that question above does not work for my situation. (And I don't have enought reputation to leave a comment there.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我是男神闪亮亮 2025-02-19 06:15:48

您可以使用 concat 作为嵌套数据框

nested_df = df.apply(lambda x: get_taxes_from_api(x["State"],x["Annual Salary"]),axis=1)

result = pd.DataFrame()

for element in nested_df:
    result = pd.concat([result,element])

结果:

print(result)
State annual.fica.amount annual.federal.amount annual.state.amount
0 New York 8899 39847 6375
1 New York 141725 172673 139201
0 New Hampshire 8623 38611 6177
1 New Hampshire 137327 167315 134881
0 California 8534 38216 6114
1 California 135922 165604 133502
0 Vermont 8147 36479 5836
1 Vermont 129746 158078 127435
0 IDAHO 8040 36003 5760
1 IDAHO 128051 156014 1257714

You could use concat for the nested DataFrame

nested_df = df.apply(lambda x: get_taxes_from_api(x["State"],x["Annual Salary"]),axis=1)

result = pd.DataFrame()

for element in nested_df:
    result = pd.concat([result,element])

result:

print(result)
State annual.fica.amount annual.federal.amount annual.state.amount
0 New York 8899 39847 6375
1 New York 141725 172673 139201
0 New Hampshire 8623 38611 6177
1 New Hampshire 137327 167315 134881
0 California 8534 38216 6114
1 California 135922 165604 133502
0 Vermont 8147 36479 5836
1 Vermont 129746 158078 127435
0 Idaho 8040 36003 5760
1 Idaho 128051 156014 125771
世界和平 2025-02-19 06:15:48

您可以在两个DFS中创建一个新的JOIN密钥,然后做PD.Merge。请参阅此处:

df["df_merge_key"] = "#"
df_after_apply["df_merge_key"] = "#"
details_df = pd.merge(df, df_after_apply, how="left", on="df_merge_key").drop(labels=["df_merge_key"], axis=1)

我认为这更简单,更整洁。

You can create a new join key among the two dfs and do pd.merge. See here:

df["df_merge_key"] = "#"
df_after_apply["df_merge_key"] = "#"
details_df = pd.merge(df, df_after_apply, how="left", on="df_merge_key").drop(labels=["df_merge_key"], axis=1)

This is simpler and neater in my opinion.

眼泪淡了忧伤 2025-02-19 06:15:47

这不是直接回答您的问题,但是这是不使用应用程序

col = ['State','Annual Salary']
dat = [['New York', 132826], ['New Hampshire',128704], ['California',127388], ['Vermont',121599], ['Idaho',120011]]
df = pd.DataFrame(dat, columns=col)

#Create the "first" row of each state from your function by adding columns
df['annual.fica.amount'] = df['Annual Salary'].multiply(0.067)
df['annual.federal.amount'] = df['Annual Salary'].multiply(0.3)
df['annual.state.amount'] = df['Annual Salary'].multiply(0.048)

#Create the "second" row of each state as a new df
cumulative_df = df.copy()
cumulative_df['annual.fica.amount'] += cumulative_df['Annual Salary']
cumulative_df['annual.federal.amount'] += cumulative_df['Annual Salary']
cumulative_df['annual.state.amount'] += cumulative_df['Annual Salary']

#Concatenate the two tables and sort so the states are right next to each other
final_df = pd.concat((df,cumulative_df)).sort_values('State').reset_index(drop=True)

输出

“在此处输入图像说明”

This doesn't directly answer your question, but here's one way that doesn't use apply

col = ['State','Annual Salary']
dat = [['New York', 132826], ['New Hampshire',128704], ['California',127388], ['Vermont',121599], ['Idaho',120011]]
df = pd.DataFrame(dat, columns=col)

#Create the "first" row of each state from your function by adding columns
df['annual.fica.amount'] = df['Annual Salary'].multiply(0.067)
df['annual.federal.amount'] = df['Annual Salary'].multiply(0.3)
df['annual.state.amount'] = df['Annual Salary'].multiply(0.048)

#Create the "second" row of each state as a new df
cumulative_df = df.copy()
cumulative_df['annual.fica.amount'] += cumulative_df['Annual Salary']
cumulative_df['annual.federal.amount'] += cumulative_df['Annual Salary']
cumulative_df['annual.state.amount'] += cumulative_df['Annual Salary']

#Concatenate the two tables and sort so the states are right next to each other
final_df = pd.concat((df,cumulative_df)).sort_values('State').reset_index(drop=True)

Output

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文