I have a "single-liner" dataframe like this:
Value 1 Value 2 Value 3
code
123 0 3 231
I want to turn the zero values into empty, so it gets to look like this:
Value 1 Value 2 Value 3
code
123 3 231
How could I do that?
In this method, we will use “df. fillna(method='ffill')” , which is used to propagate non-null values forward or backward.
You can also use df. replace(np. nan,0) to replace all NaN values with zero. This replaces all columns of DataFrame with zero for Nan values.
You can use pandas.DataFrame.eq
to find the location of all the zero values across the entire DataFrame, and then replace this with something else, maybe np.nan
.
import numpy as np
df[df.eq(0)] = np.nan
Though if you have only one row, you can also replace it with an empty string since you (probably) don't have to worry about the dtype
for the column changing:
df[df.eq(0)] = ''
The code below reads like so: set the column 'Value 1' equal to nothing where column 'Value 1' is equal to 0.
df.loc[df['Value 1'] == 0, 'Value 1'] = ''
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