Fillna in multiple columns in place in Python Pandas
Solution 1:
Came across this page while looking for an answer to this problem, but didn't like the existing answers. I ended up finding something better in the DataFrame.fillna documentation, and figured I'd contribute for anyone else that happens upon this.
If you have multiple columns, but only want to replace the NaN
in a subset of them, you can use:
df.fillna({'Name':'.', 'City':'.'}, inplace=True)
This also allows you to specify different replacements for each column. And if you want to go ahead and fill all remaining NaN
values, you can just throw another fillna
on the end:
df = df.fillna({'Name':'.', 'City':'.'}).fillna(0)
Edit: Functionality changed since original post, and you can no longer chain 2 inplace
fillna()
operations. You can still chain, but now must assign that chain to the df
instead of modifying in place.
Solution 2:
You could use apply
for your columns with checking dtype
whether it's numeric
or not by checking dtype.kind
:
res = df.apply(lambda x: x.fillna(0) if x.dtype.kind in 'biufc' else x.fillna('.'))
print(res)
A B City Name
0 1.0 0.25 Seattle Jack
1 2.1 0.00 SF Sue
2 0.0 0.00 LA .
3 4.7 4.00 OC Bob
4 5.6 12.20 . Alice
5 6.8 14.40 . John
Solution 3:
You can either list the string columns by hand or glean them from df.dtypes
. Once you have the list of string/object columns, you can call fillna
on all those columns at once.
# str_cols = ['Name','City']
str_cols = df.columns[df.dtypes==object]
df[str_cols] = df[str_cols].fillna('.')
df = df.fillna(0)