How to fill NaN only in numeric variables if that variable in on list in Python Pandas?

Question:

I have Pandas DataFrame like below:

data types:

  • COL1 – numeric
  • COL2 – object
  • COL3 – numeric

TABLE 1

COL1 | COL2 | COL3 
-----|------|------
123  | AAA  | 99
NaN  | ABC  | 1
111  | NaN  | NaN
...  | ...  | ...

And I have also list of variables like that: my_list = ["COL1", "COL8", "COL15"]

And I need to fill NaN by 0 under below conditions:

  • if some column from TABLE 1 is numeric
  • if some column from TABLE 1 has NaN
  • if some column From TABLE 1 is on my_list

So, I need something like below as an output, because only COL1 meet all above requirements:

COL1 | COL2 | COL3 | COL4
-----|------|------|-------
123  | AAA  | 99   | XC
0    | ABC  | 1    | XB
111  | NaN  | NaN  | XA
...  | ...  | ...  | ...

How can I do that in Python Pandas ?

Asked By: dingaro

||

Answers:

You can use a combination of Index.intersection and select_dtypes to select the columns in which to fillna, then update:

df.update(df[df.columns.intersection(my_list)].select_dtypes('number').fillna(0))
Answered By: mozway