Reputation: 2342
I have Pandas DataFrame like below:
data types:
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:
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 ?
Upvotes: 1
Views: 322
Reputation: 260480
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))
Upvotes: 1