I know I can replace all nan values with df.fillna(0)
and replace a single value with df.replace('-',1)
, but how can I replace all non-zero values with a single value?
You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.
Use boolean indexing:
df[df != 0] = value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With