Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to solve a KeyError problem in Pandas

enter code here

           Gender   Value   Year    Measure
Name                
Ma         girl     0       1997    Rank
Nabiha     girl     1204    2014    Rank
Muad.      boy      1749    2009    Rank
Zynah      girl     4763    2017    Rank
Aathavi    girl     0       2019    Rank
Kia-Marie  girl     0       2017    Count
Tishaan    boy      0       2006    Rank
Ryyan      boy      0       2010    Count
Llyr       boy      1852    2012    Rank
Abdirazaq  boy      0       2017    Rank

The way that rank is calculated, I created a function so that those with s[‘Measure’] == "Count" and s[‘Value’] == 0 could be adjusted so they were ranked bottom not 0 as top value.

def adjust_rank(s):
    if s['Value'] == 0 & s['Measure'] == "Count":
        if s["Gender"] == "boy":
            s['Rank'] = 5000 # Bottom possible rank
        elif s["Gender"] == "girl":
            s['Rank'] = 3459 # Bottom possible rank
        else:
            s['Rank'] = s['Value']
df['Rank'] = df.apply(adjust_rank)

Why do I get: KeyError: ‘Value’?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use numpy.select:

m = (df['Value'] == 0) & (df['Measure'] == "Count")
m1 = df["Gender"] == "boy"
m2 = df["Gender"] == "girl"

df['Rank'] = np.select([m & m1, m & m2, m & ~(m1 | m2)], 
                       [5000, 3459, df['Value']], default=None)

print (df)
          Gender  Value  Year Measure  Rank
Ma          girl      0  1997    Rank  None
Nabiha      girl   1204  2014    Rank  None
Muad.        boy   1749  2009    Rank  None
Zynah       girl   4763  2017    Rank  None
Aathavi     girl      0  2019    Rank  None
Kia-Marie   girl      0  2017   Count  3459
Tishaan      boy      0  2006    Rank  None
Ryyan        boy      0  2010   Count  5000
Llyr         boy   1852  2012    Rank  None
Abdirazaq    boy      0  2017    Rank  None

Or maybe need:

m = (df['Value'] == 0) & (df['Measure'] == "Count")
m1 = df["Gender"] == "boy"
m2 = df["Gender"] == "girl"

df['Rank'] = np.select([m & m1, m & m2], [5000, 3459], df['Value'])


print (df)
          Gender  Value  Year Measure  Rank
Ma          girl      0  1997    Rank     0
Nabiha      girl   1204  2014    Rank  1204
Muad.        boy   1749  2009    Rank  1749
Zynah       girl   4763  2017    Rank  4763
Aathavi     girl      0  2019    Rank     0
Kia-Marie   girl      0  2017   Count  3459
Tishaan      boy      0  2006    Rank     0
Ryyan        boy      0  2010   Count  5000
Llyr         boy   1852  2012    Rank  1852
Abdirazaq    boy      0  2017    Rank     0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading