'Pandas datafram fillna setting value based on datatype

I have a pandas dataframe that has data extract from a table. The table can be any given table from the database. The table may have unknown number of columns and datatypes. The database I am using is Redshift.

After get the data in pandas DF, I need to check for nan values in numeric/decimal type of columns if there are any columns with nan value, I need to change the value to 0

col_list_nan = pd.columns[pdf.isna().any()].tolist()

for i in col_list_nan:
  pdf = pdf[i].replace(np.nan, 0)

How can I change the column value and return complete dataframe with remaining unchanged columns and with the replaced columns that have 0 instead of nan



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source