I have the following data sample. I would like to
- a) in column C, replace the
np.NaN with 999, - b) in column D, place '' with
np.NaN.
Neither of my attempts is working, and I am not sure why.
import pandas
from pandas import DataFrame
import numpy as np
df = DataFrame({'A' : ['foo', 'foo', 'foo', 'foo',
'bar', 'bar', 'bar', 'bar'],
'B' : ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three'],
'C' : [1, np.NaN, 1, 2, np.NaN, 1, 1, 2], 'D' : [2, '', 1, 1, '', 2, 2, 1]})
print df
df.C.fillna(999)
df.D.replace('', np.NaN)
print df
Output:
A B C D
0 foo one 1 2
1 foo one NaN
2 foo two 1 1
3 foo three 2 1
4 bar two NaN
5 bar two 1 2
6 bar one 1 2
7 bar three 2 1
A B C D
0 foo one 1 2
1 foo one NaN
2 foo two 1 1
3 foo three 2 1
4 bar two NaN
5 bar two 1 2
6 bar one 1 2
7 bar three 2 1