RajeshM
RajeshM

Reputation: 89

Missing Values Treatment Pandas

I have values in two columns as below: '''

    column1              Column2                                                                    
    xyz                   xyzabc                                                                                    



    qwe                  qwerty 
    

'''

I want to fill remaining rows with forward filling method. But Even after using following code its showing nothing.(column showing no null values when used df.info, though it is having)(when I used inplace=True its showing None entries in whole column1)

'''

    df["column1"].fillna( method ='ffill')
    df["column2"].fillna(method = 'ffill', inplace = True)

'''

Upvotes: 1

Views: 104

Answers (1)

jezrael
jezrael

Reputation: 863701

Repalce empty or whitespaces to missing values first:

df = pd.DataFrame({'User': ['acanter ', ' ', ' ', ' ', ' '], 
 'Name': [' Andy Canter ', ' ', ' ', ' ', ' '],
 'Company': [' 135 ', ' 135 ', ' 135 ', ' 000 ', ' 135 '],
 'Session': [' ottstpbdeman ', ' ttstptcserver ', ' ottstpjcadaem ', ' ottstpstdlib ', ' bptmmo486m000 '],
 'Description': [' ttstpbdeman ', ' Thin Client server ', ' ttstpjcadaemo ', ' stdlib Server ', 'new']})

print (df)
       User           Name Company          Session           Description
0  acanter    Andy Canter     135     ottstpbdeman           ttstpbdeman 
1  acanter    Andy Canter     135    ttstptcserver    Thin Client server 
2  acanter    Andy Canter     135    ottstpjcadaem         ttstpjcadaemo 
3  acanter    Andy Canter     000     ottstpstdlib         stdlib Server 
4  acanter    Andy Canter     135    bptmmo486m000                    new

df = df.replace(r'^\s*$', np.nan, regex=True).ffill()
print (df)
       User           Name Company          Session           Description
0  acanter    Andy Canter     135     ottstpbdeman           ttstpbdeman 
1  acanter    Andy Canter     135    ttstptcserver    Thin Client server 
2  acanter    Andy Canter     135    ottstpjcadaem         ttstpjcadaemo 
3  acanter    Andy Canter     000     ottstpstdlib         stdlib Server 
4  acanter    Andy Canter     135    bptmmo486m000                    new

Upvotes: 2

Related Questions