Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas replace values by their opposite sign

I am trying to "clean" some data. I have values which are negative, which they cannot be. And I would like to replace all values that are negative to their corresponding positive values.

A    | B     | C
-1.9 | -0.2  | 'Hello'
1.2  | 0.3   | 'World'

I would like this to become

A    | B     | C
1.9  | 0.2   | 'Hello'
1.2  | 0.3   | 'World'

As of now I have just begun writing the replace statement

df.replace(df.loc[(df['A'] < 0) & (df['B'] < 0)],df * -1,inplace=True)

Please help me in the right direction

like image 770
eleijonmarck Avatar asked Mar 27 '15 11:03

eleijonmarck


People also ask

How do you reverse in Pandas?

Using loc() function to Reverse Row Reversing the rows of a data frame in pandas can be done in python by invoking the loc() function. The panda's dataframe. loc() attribute accesses a set of rows and columns in the given data frame by either a label or a boolean array.

How replace values in column based on multiple conditions in Pandas?

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.

How do I replace values in a pandas Dataframe?

In this post, you learned how to use the Pandas replace method to, well, replace values in a Pandas dataframe. The .replace () method is extremely powerful and lets you replace values across a single column, multiple columns, and an entire dataframe. The method also incorporates regular expressions to make complex replacements easier.

How to replace special characters with regex and replace in pandas?

Regex replace special characters - r' [^0-9a-zA-Z:,\s]+' - including spaces df['internship'].str.replace(r' [^0-9a-zA-Z:,]+', '') In the next part of the post, you'll see the steps and practical examples on how to use regex and replace in Pandas. First, let's create a sample 'dirty' data which needs to be cleaned and replaced:

How to replace all non-numeric symbols in a string?

This is done on three simple steps: 1 first replace all non numeric symbols - str.replace (r'D+', '', regex=True) 2 second - in case of missing numbers - empty string is returned - map the empty string to 0 by .replace ( {'':0}) 3 convert to numeric column More ...

How do I replace a list with multiple values in Python?

Similar to the example above, you can replace a list of multiple values with a list of different values. This is as easy as loading in a list into each of the to_replace and values parameters. It’s important to note that the lists must be the same length.


Video Answer


2 Answers

Just call abs:

In [349]:

df = df.abs()
df
Out[349]:
     A    B
0  1.9  0.2
1  1.2  0.3

Another method would be to create a boolean mask, drop the NaN rows, call loc on the index and assign the negative values:

df.loc[df[df<0].dropna().index] = -df

EDIT

For the situation where you have strings the following would work:

In [399]:

df[df.columns[df.dtypes != np.object]] = df[df.columns[df.dtypes != np.object]].abs()
df
Out[399]:
     A    B      C
0  1.9  0.2  Hello
1  1.2  0.3  World
like image 161
EdChum Avatar answered Sep 20 '22 13:09

EdChum


You can be use this way:

first make column as a string:

df['A']=df['A'].astype('str')

df['B']=df['B'].astype('str')

Then use replace function:

df['A']=df['A'].str.replace('-','')

df['B']=df['B'].str.replace('-','')

then make it as float data type:

df['A']=df['A'].astype('float')
df['B']=df['B'].astype('float')

I think this will be help you in this problem.

like image 41
Akash Nayak Avatar answered Sep 22 '22 13:09

Akash Nayak