Python Forum
Difference of two columns in Pandas dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Difference of two columns in Pandas dataframe
#1
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
Reply
#2
Hi any help?

this is my cross-post
https://stackoverflow.com/questions/6242...-dataframe
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add columns to polars dataframe sayyedkamran 1 1,619 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  concat 3 columns of dataframe to one column flash77 2 752 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 931 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,080 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,381 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,251 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,229 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,745 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,515 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,037 Aug-14-2021, 12:38 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020