Reputation: 4636
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?
Upvotes: 10
Views: 18717
Reputation: 43504
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)] = ''
Upvotes: 10
Reputation: 2286
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'] = ''
Upvotes: 2