Python Forum
pandas dataframe substracting columns: key error
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas dataframe substracting columns: key error
#1
Hi Pandas Experts,

I got a dataframe that has a country and its GDP for the year 2015 up to 2006

    newfour = newtwo[(newtwo['2015']-newtwo['2006'])]
When I try to substract 2015 from 2006 and ingone the rest I read the error:

KeyError: '[  4.80566748e+12   1.75626949e+12   1.73021626e+11   2.46702696e+11\n   2.30356251e+11   2.28139911e+11   3.52665152e+11   1.10131180e+12\n   1.53345695e+11   3.25560528e+11  -1.52854060e+11   4.99867550e+09\n              nan   2.79311766e+11   4.74343726e+11] not in index'

It seems the GDP values are not an/in index but I wonder why that is required?
Why does a column need to be an index to be subtracted from another?
Many thanks for any explanations
Reply
#2
Well, I am not entirely sure what do you want to achieve, but if you just need to count difference of two columns of pandas' dataframe,  then subtract them.

There is no need for your outer newtwo[...] selection , you are trying to use your computed differences as a keys and that wont work.

Output:
In [1]: import pandas as pd In [2]: newtwo = pd.DataFrame({'astate':['first', 'second'], 'gdp2010':[113.2, 341.1], 'gdp2015':[233.1, 417.1]}) In [3]: newtwo Out[3]:    astate  gdp2010  gdp2015 0   first    113.2    233.1 1  second    341.1    417.1 In [4]: newfour = newtwo['gdp2015'] - newtwo['gdp2010'] In [5]: newfour Out[5]: 0    119.9 1     76.0 dtype: float64 In [6]: newtwo[(newtwo['gdp2010'] - newtwo['gdp2015'])]    # same as newtwo[[-119.9, -76.0]] and that doesn't make sense ... ... KeyError: '[-119.9  -76. ] not in index'
You can find a nice short introduction to pandas on offical pandas page.
Reply
#3
(Feb-23-2017, 01:41 PM)zivoni Wrote: There is no need for your outer newtwo[...] selection , you are trying to use your computed differences as a keys and that wont work.

I see. That was it! I still get confused with this key and index thing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas dataframe indexing pythonNovice 1 966 Jun-16-2022, 04:43 PM
Last Post: jefsummers
  How can I convert specific rows from excel to pandas dataframe? mcva 1 1,765 Apr-20-2020, 09:14 AM
Last Post: pyzyx3qwerty
  Histogram using pandas dataframe not showing proper output ift38375 1 2,150 Jul-04-2019, 10:43 PM
Last Post: scidam
  Need help getting unique values across two columns of a dataframe a_real_phoenix 1 1,790 Jun-30-2019, 01:46 AM
Last Post: scidam
  Pass 2 columns via loc to lambda in pandas fad3r 1 7,878 Feb-22-2018, 09:57 PM
Last Post: glidecode
  Substracting number with a limit mrplow92 3 3,035 Feb-01-2018, 06:04 PM
Last Post: gruntfutuk
  pandas dataframe next rows value metalray 2 10,102 Mar-06-2017, 11:31 AM
Last Post: metalray
  pandas dataframe group by count index metalray 5 10,187 Mar-01-2017, 09:14 AM
Last Post: metalray
  pandas dataframe.replace regex metalray 3 24,538 Feb-24-2017, 12:58 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

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