Replacing blank values (white space) with NaN in pandas
--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping
--
Chapters
00:00 Replacing Blank Values (White Space) With Nan In Pandas
01:05 Answer 1 Score 40
01:18 Accepted Answer Score 349
01:56 Answer 3 Score 42
02:14 Answer 4 Score 122
02:53 Thank you
--
Full question
https://stackoverflow.com/questions/1344...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #dataframe
#avk47
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping
--
Chapters
00:00 Replacing Blank Values (White Space) With Nan In Pandas
01:05 Answer 1 Score 40
01:18 Accepted Answer Score 349
01:56 Answer 3 Score 42
02:14 Answer 4 Score 122
02:53 Thank you
--
Full question
https://stackoverflow.com/questions/1344...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pandas #dataframe
#avk47
ACCEPTED ANSWER
Score 349
I think df.replace() does the job, since pandas 0.13:
df = pd.DataFrame([
[-0.532681, 'foo', 0],
[1.490752, 'bar', 1],
[-1.387326, 'foo', 2],
[0.814772, 'baz', ' '],
[-0.222552, ' ', 4],
[-1.176781, 'qux', ' '],
], columns='A B C'.split(), index=pd.date_range('2000-01-01','2000-01-06'))
# replace field that's entirely space (or empty) with NaN
print(df.replace(r'^\s*$', np.nan, regex=True))
Produces:
A B C
2000-01-01 -0.532681 foo 0
2000-01-02 1.490752 bar 1
2000-01-03 -1.387326 foo 2
2000-01-04 0.814772 baz NaN
2000-01-05 -0.222552 NaN 4
2000-01-06 -1.176781 qux NaN
As Temak pointed it out, use df.replace(r'^\s+$', np.nan, regex=True) in case your valid data contains white spaces.
ANSWER 2
Score 122
If you want to replace an empty string and records with only spaces, the correct answer is!:
df = df.replace(r'^\s*$', np.nan, regex=True)
The accepted answer
df.replace(r'\s+', np.nan, regex=True)
Does not replace an empty string!, you can try yourself with the given example slightly updated:
df = pd.DataFrame([
[-0.532681, 'foo', 0],
[1.490752, 'bar', 1],
[-1.387326, 'fo o', 2],
[0.814772, 'baz', ' '],
[-0.222552, ' ', 4],
[-1.176781, 'qux', ''],
], columns='A B C'.split(), index=pd.date_range('2000-01-01','2000-01-06'))
Note, also that 'fo o' is not replaced with Nan, though it contains a space. Further note, that a simple:
df.replace(r'', np.NaN)
Does not work either - try it out.
ANSWER 3
Score 42
I did this:
df = df.apply(lambda x: x.str.strip()).replace('', np.nan)
or
df = df.apply(lambda x: x.str.strip() if isinstance(x, str) else x).replace('', np.nan)
You can strip all str, then replace empty str with np.nan.
ANSWER 4
Score 40
How about:
d = d.applymap(lambda x: np.nan if isinstance(x, basestring) and x.isspace() else x)
The applymap function applies a function to every cell of the dataframe.