Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Removed <NA> from dataframe

Hi all

 

I am new to python and I am looking at the regression modelling, I am trying to find out how I can remove the <NA> from the Local Authority column in python.

 

I have tried 

 

df = df.dropna

df = df[Local Authority] != ''

df = df.[Local Authority] != '<NA>'

 

And others but keep getting errors or the values remain. 

 

Table below is an example of what I am trying to remove.

 

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi Anonymous 

     

    ibarrau Thanks for your sharing!

     

    After testing, I offer an alternative approach:

     

    local_authority_index = 1 #The number here is determined by the column where your <NA> is located.
    for row in df[1:]:
        row[localauthority_index] = None

     

    Here is the result:

     

    Regards,

    Nono Chen

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

2 Replies

  • Hi. That <NA> doesn't look like a traditional nan that can be droped with the functions, it just looks like regular text. Have you tried to replace it? Somethig like replacing that for real none value:

    df['Local Authority'] = df['Local Authority'].replace('<NA>', None)

    I haven't tested but I think that might work. If you have more <NA> in other columns you can write the same code without the columns at df.

    I hope that helps,

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous 

     

    ibarrau Thanks for your sharing!

     

    After testing, I offer an alternative approach:

     

    local_authority_index = 1 #The number here is determined by the column where your <NA> is located.
    for row in df[1:]:
        row[localauthority_index] = None

     

    Here is the result:

     

    Regards,

    Nono Chen

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.