BDK
BDK

Reputation: 21

Replace '-' but not negative numbers in pandas

In a DataFrame, I have negative numbers, and also missing values that are given by a - . I want to replace the missing values with an empty cell, but this operation should NOT remove the - in front of the negative numbers.

It looks like:

45      45      45      45      45      45      45      45      45      45
45      45      15      31      43      45      45      45      45      45
44.24   121.55  1.80    0.00%   -       97.63   -4.87   -6.02   -20.14  169.19
1       1       7       12      3       1       1       1       1       1

So the missing value cell with the - should be empty, but the -4.87 should stay intact.

Any help would be greatly appreciated.

Upvotes: 1

Views: 118

Answers (1)

DYZ
DYZ

Reputation: 57033

The problem should have been addressed at the time of loading the file into the DataFrame (by providing the na_values parameter to read_csv() or whatever function you used).

At this point, use operation replace(): it replaces whole words, not individual characters.

df = df.replace("-", np.nan)

Upvotes: 1

Related Questions