gpt4 book ai didi

python - 匹配 Excel 文件中的两列并获取其他列值 - Python Pandas

转载 作者:行者123 更新时间:2023-12-01 01:55:12 25 4
gpt4 key购买 nike

我有两个 Excel 文件,例如 wb1.xlsxwb2.xlsx

wb1.xlsx

adsl    svc_no    port_stat    adsl.1    Comparison result
2/17
2/24
2/27
2/33
2/37
3/12

wb2.xlsx

caller_id    status    adsl    Comparison result
n/a SP 2/37 Not Match
n/a RE 2/24 Not Match
n/a SP 2/27 Match
n/a SP 2/33 Not Match
n/a SP 2/17 Match

我想要做的是将 wb2.xlsx 的 adsl 与 wb1.xlsx 匹配,并将其他值获取到其他列。

我的预期输出是使用 wb2.xlsx 中的值更新 wb1.xlsx

adsl    svc_no    port_stat    adsl.1    Comparison result
2/17 n/a SP 2/17 Match
2/24 n/a RE 2/24 Not Match
2/27 n/a SP 2/27 Match
2/33 n/a SP 2/33 Not Match
2/37 n/a SP 2/37 Not Match
3/12

经过搜索,我能够检查 pd.merge() 是否能够进行匹配。

我试过这样:

result = pd.merge(df2, pri_df, on=['adsl', 'adsl'])

不幸的是,它创建了新列并且不更新现有列。此外,它只获取它能够匹配的值,而忽略其他行。

我还尝试获取 wb2.xlsx 中列的索引并将其分配给 wb1.xlsx 列,但它只是逐字复制。

任何有帮助的引用都可以。

最佳答案

我建议使用intersectioncombine_first :

print (df1)
adsl svc_no port_stat adsl.1 Comparison result
0 2/17 NaN NaN NaN NaN
1 2/24 NaN NaN NaN NaN
2 2/27 NaN NaN NaN NaN
3 2/33 NaN NaN NaN NaN
4 2/37 NaN NaN NaN NaN
5 3/12 NaN NaN NaN NaN

print (df2)
caller_id port_stat adsl Comparison result
0 NaN SP 2/37 Not Match
1 NaN RE 2/24 Not Match
2 NaN SP 2/27 Match
3 NaN SP 2/33 Not Match
4 NaN SP 2/17 Match
<小时/>
df2 = df2.rename(columns={'status':'port_stat'})
d = {'adsl.1': lambda x: x['adsl']}
df2 = df2.assign(**d)
print (df2)
caller_id port_stat adsl Comparison result adsl.1
0 NaN SP 2/37 Not Match 2/37
1 NaN RE 2/24 Not Match 2/24
2 NaN SP 2/27 Match 2/27
3 NaN SP 2/33 Not Match 2/33
4 NaN SP 2/17 Match 2/17

df22 = df2[df2.columns.intersection(df1.columns)]
print (df22)
port_stat adsl Comparison result adsl.1
0 SP 2/37 Not Match 2/37
1 RE 2/24 Not Match 2/24
2 SP 2/27 Match 2/27
3 SP 2/33 Not Match 2/33
4 SP 2/17 Match 2/17

result = (df22.set_index('adsl')
.combine_first(df1.set_index('adsl'))
.reset_index()
.reindex(columns=df1.columns))
print (result)
adsl svc_no port_stat adsl.1 Comparison result
0 2/17 NaN SP 2/17 Match
1 2/24 NaN RE 2/24 Not Match
2 2/27 NaN SP 2/27 Match
3 2/33 NaN SP 2/33 Not Match
4 2/37 NaN SP 2/37 Not Match
5 3/12 NaN NaN NaN NaN

关于python - 匹配 Excel 文件中的两列并获取其他列值 - Python Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50286206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com