1

I have dataframe with values Na, blank and others. I want to replace Na with noted (string value) I want to transform from here to here without changing blank cell.

I already tried

df['A']=df['A'].replace(regex=['NaN'], value='needed')

and

df['A'].replace(regex=['NA'], value='noted
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
Kiran Gopi
  • 35
  • 6

2 Answers2

1

You can use fillna():

df['A'].fillna('noted')

Alternatively, if NA is a string and not np.nan then you can use replace():

df['A'].replace(['NA'], 'noted')
Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156
0

You can use the fillna() method -

df['A'].fillna('Noted', inplace=True)

OR

df['A'] = df['A'].fillna('Noted')

To change without changing blanks - you can use a mapping function

def fillna_not_blanks(value):
    if value.strip() == '':
        return value
    elif value == np.nan:
        return 'Noted'
    else:
        return value

df['A'] = df['A'].map(fillna_not_blanks)
Mortz
  • 4,654
  • 1
  • 19
  • 35