gpt4 book ai didi

python - 比较两个数据帧、一列,并在匹配时添加某些值?

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

所以我有两个数据框

eqdf
symbol qty
0 DABIND 1
1 INFTEC 6
2 DISHTV 8
3 HINDAL 40
4 NATMIN 5
5 POWGRI 40
6 CHEPET 6

premdf
share strike lprice premperc d_strike
0 HINDAL 250.0 237.90 1.975620 5.086171
1 RELIND 1280.0 1254.30 1.642350 2.048952
2 POWGRI 205.0 201.15 1.118568 1.913995

我想比较 premdf['share']eqdf['symbol'] 以及是否存在匹配prempercd_strikestrike 值将添加到存在匹配的 eqdf 行的末尾。

我已经尝试过

eqdf.loc[eqdf['symbol']==premdf['share'],eqdf['premperc'] == premdf['premperc']]

我不断收到错误

ValueError: Can only compare identically-labeled Series objects

预期输出:

eqdf
symbol qty premperc d_strike strike
0 DABIND 1 NaN NaN NaN
1 INFTEC 6 NaN NaN NaN
2 DISHTV 8 NaN NaN NaN
3 HINDAL 40 1.975620 5.086171 250.0
4 NATMIN 5 NaN NaN NaN
5 POWGRI 40 1.118568 1.913995 205.0
6 CHEPET 6 NaN NaN NaN

正确的做法是什么?

谢谢

最佳答案

重命名合并

eqdf.merge(premdf.rename(columns={'share': 'symbol'}), 'left')

symbol qty strike lprice premperc d_strike
0 DABIND 1 NaN NaN NaN NaN
1 INFTEC 6 NaN NaN NaN NaN
2 DISHTV 8 NaN NaN NaN NaN
3 HINDAL 40 250.0 237.90 1.975620 5.086171
4 NATMIN 5 NaN NaN NaN NaN
5 POWGRI 40 205.0 201.15 1.118568 1.913995
6 CHEPET 6 NaN NaN NaN NaN

关于python - 比较两个数据帧、一列,并在匹配时添加某些值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52122399/

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