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
  update values in one dataframe based on another dataframe - Pandas iliasb 2 669 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 606 Jun-22-2021, 07:57 PM
Last Post: snippsat
  Convert several columns to int in dataframe Krayna 2 497 May-21-2021, 08:55 AM
Last Post: Krayna
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 769 May-09-2021, 06:46 PM
Last Post: Pit292
  pandas.to_datetime: Combine data from 2 columns ju21878436312 1 836 Feb-20-2021, 08:25 PM
Last Post: perfringo
  Remove extra count columns created by pandas groupby spyf8 1 904 Feb-10-2021, 09:19 AM
Last Post: Naheed
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 1,069 Jan-27-2021, 10:59 AM
Last Post: epsilon
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 921 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Comparing results within a list and appending to pandas dataframe Aryagm 1 738 Dec-17-2020, 01:08 PM
Last Post: palladium
  How to search for specific string in Pandas dataframe Coding_Jam 1 985 Nov-02-2020, 09:35 AM
Last Post: PsyPy

Forum Jump:

User Panel Messages

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