Home > Back-end >  Python / pandas function to replace '-' negative sign but not negative sign in negative nu
Python / pandas function to replace '-' negative sign but not negative sign in negative nu

Time:01-23

I have a pandas dataframe that contains - in missing value, If I use .replace('-','NaN') it replaces -1 with NaN1 how can I remove only - sign but not - if its -1

Dataframe Example

Co1_1 Values
Pine  -
Apple -1
Mango 2
Berry -
Banana -3

Here I want to replace - in Pine & Berry not in banana

CodePudding user response:

There is no need for .replace() at all. Find the cells that contain the dash, and update them:

df[df['Values'] == '-'] = np.nan

Bear in mind that 'NaN' is not a NaN: it is a string that looks like a NaN. A "real" NaN is np.nan from numpy.

CodePudding user response:

You can cast the value of the column as numeric. Then maybe - will be replaces by 0, not sure if you want it :

pandas.to_numeric(arg, errors='raise', downcast=None)

Reference


If it doesn't work, astype may be an alternative

DataFrame.astype(dtype, copy=True, errors='raise')
# Example :
df.astype({'values': int})

Reference


Nevertheless the most elegant solution, and the most often used is to use th pandas selector:

df[ df['Values'] == '-' ] = np.nan

Also you can choose to replace the value - by anything you want: 0, Nan (np.nan), etc.

CodePudding user response:

So, if you first evaluate the length of the value you are going to check (converted to a string), than you could be sure to only select the '-' entries, when you just delete entries with length of one.

for values:
    if len(str(value)) == 1:
        value.replace(...)

But I'm shure there is a more elegant way ;D

  •  Tags:  
  • Related