Search code examples
pythonpandasxlwings

Difference of two columns in Pandas dataframe


Hi I tring to get two columns from Excel and parse to a DataFrame, after that I need subtract this columns.

This is my code

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))

Cross post in : https://python-forum.io/Thread-Difference-of-two-columns-in-Pandas-dataframe


Solution

  • One suggestion to make it work is to add condition index=False to options function.

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

    xw has parsed your column VC as index, which caused KeyError when you try to operate on that column.