数据帧中两列的差异

2024-09-27 00:14:37 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我尝试从Excel中获取两列并解析为一个数据帧,然后我需要减去这两列

这是我的密码

ndf = xw.Range('AI1:AJ' + str(len(last_row))).options(pd.DataFrame).value

#Error in this line below
ndf['VC-BC'] = ndf['VC'] - ndf['BC']

#xw.Range("BH1").options(index=False).value = ndf

print(ndf.head(20))

交叉邮寄: https://python-forum.io/Thread-Difference-of-two-columns-in-Pandas-dataframe


Tags: 数据in密码lenvaluerangeexceloptions
1条回答
网友
1楼 · 发布于 2024-09-27 00:14:37

一个建议是将条件index=False添加到options函数中

ndf = xw.Range('AI1:AJ' + str(len(last_row))).options(pd.DataFrame, index=False).value

xw已将您的列VC解析为索引,这在您尝试对该列进行操作时导致KeyError

相关问题 更多 >

    热门问题