Python Forum
Difference of two columns in Pandas dataframe - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Difference of two columns in Pandas dataframe (/thread-27277.html)



Difference of two columns in Pandas dataframe - zinho - Jun-01-2020

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

        
    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))
After run code error appear
Error:
C:\Users\alexandre.goncalves>C:/Users/alexandre.goncalves/AppData/Local/Programs /Python/Python38-32/python.exe c:/Users/zinho/Downloads/projetos/f at_vfour.py Traceback (most recent call last): File "C:\Users\zinho\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc return self._engine.get_loc(key) File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 1618, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 1626, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'VC' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:/Users/zinho/Downloads/projetos/fat_vfour.py", line 90, in <module> ndf['VC-BC'] = ndf['VC'] - ndf['BC'] File "C:\Users\zinho\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__ indexer = self.columns.get_loc(key) File "C:\Users\zinho\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key)) File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc File "pandas\_libs\hashtable_class_helper.pxi", line 1618, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 1626, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'VC'
If try show my new dataframe display like this.
Output:
BC VC 1845.60 0.00 963.72 0.00 1898.00 0.00 15.43 5.56 8.07 2.91 4.99 1.79 41.18 14.75 34.14 12.30 77.32 21.65 107.28 107.28 7.00 2.51 18.50 6.63 6.88 2.47 19.39 6.95 27.96 10.08 51.55 14.43 26.86 9.62 108.02 38.93 98.10 35.15 108.85 39.23



RE: Difference of two columns in Pandas dataframe - zinho - Jun-17-2020

Hi any help?

this is my cross-post
https://stackoverflow.com/questions/62429862/difference-of-two-columns-in-pandas-dataframe


RE: Difference of two columns in Pandas dataframe - zinho - Jun-17-2020

My question is solved now, thank you
I miss this
ndf = xw.Range('AI1:AJ' + str(len(last_row))).options(pd.DataFrame).value
Would be this
ndf = xw.Range('AI1:AJ' + str(len(last_row))).options(pd.DataFrame, index=False).value