If you want to fill NaN values with zeros in Python using pandas, you can use the fillna()
function.
Here is an example of how to fill NaN values with zeros in a DataFrame:
main.py209 chars11 lines
Output:
main.py72 chars7 lines
In this example, we first create a DataFrame with NaN values. Then, we use the fillna()
function and pass 0
as the argument to fill all NaN values in the DataFrame with zeros.
Note that the original DataFrame df
is modified in-place. If you want to create a new DataFrame without modifying the original one, you can use the fillna()
function with the inplace=False
parameter:
main.py37 chars2 lines
I hope this helps!
gistlibby LogSnag