根据值将 df1 列与 df2 列交换
目标:基于 'df_state.abbr` 将 df_hsa.stateabbr
替换为 df_state.state
。
是否有这样的函数,我在其中提到源、目标和基于数据框的列?
我需要以类似的方式订购两个 DataFrame 吗?
df_hsa
:
hsa stateabbr county
0 259 AL Butler
1 177 AL Calhoun
2 177 AL Cleburne
3 172 AL Chambers
4 172 AL Randolph
df_state
:
abbr state
0 AL Alabama
1 AK Alaska
2 AZ Arizona
3 AR Arkansas
4 CA California
所需输出: df_hsa
与 state
列而不是 stateabbr
。
hsa state county
0 259 Alabama Butler
1 177 Alabama Calhoun
2 177 Alabama Cleburne
3 172 Alabama Chambers
4 172 Alabama Randolph
Goal: swap out df_hsa.stateabbr
with df_state.state
, based on 'df_state.abbr`.
Is there such a function, where I mention source, destination, and based-on dataframe columns?
Do I need to order both DataFrames similarly?
df_hsa
:
hsa stateabbr county
0 259 AL Butler
1 177 AL Calhoun
2 177 AL Cleburne
3 172 AL Chambers
4 172 AL Randolph
df_state
:
abbr state
0 AL Alabama
1 AK Alaska
2 AZ Arizona
3 AR Arkansas
4 CA California
Desired Output:df_hsa
with state
column instead of stateabbr
.
hsa state county
0 259 Alabama Butler
1 177 Alabama Calhoun
2 177 Alabama Cleburne
3 172 Alabama Chambers
4 172 Alabama Randolph
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在将索引设置为“stateabbr”
输出后简单地加入:
如果您还想要原始索引,可以在行末尾添加
.set_index(df_hsa.index)
you can simply join after setting the index to be "stateabbr"
output:
if you also want the original index your can add
.set_index(df_hsa.index)
at the end of the line