Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing all negative values in certain columns by another value in Pandas

Suppose I have four successively arranged columns as a part of a data frame and I want to replace all the negative values in these 4 columns by another value (-5 let's say), how do I do it?

T1   T2  T3  T4
20   -5  4   3
85  -78  34  21
-45  22  31  75
-6   5   7  -28

Logically, I was hoping this would work. But, it doesn't.

for i in df.iloc[:,df.columns.get_loc("T1"):df.columns.get_loc("T1")+4]<0:
    for j in df[i]:
        if j<0:
            j=-5
like image 784
hegdep Avatar asked May 03 '18 06:05

hegdep


People also ask

How do I replace values in multiple columns in pandas?

To replace multiple values in a DataFrame, you can use DataFrame. replace() method with a dictionary of different replacements passed as argument.

How can I replace all values in a DataFrame with another value?

Suppose that you want to replace multiple values with multiple new values for an individual DataFrame column. In that case, you may use this template: df['column name'] = df['column name']. replace(['1st old value', '2nd old value', ...], ['1st new value', '2nd new value', ...])

How do you replace values in a column with 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.


2 Answers

You can just use indexing by applying a condition statement.

cols = ['T1','T2','T3','T4']
df[df[cols] < 0] = -5

Output

In [35]: df
Out[35]:
     T1  T2  T3  T4
  0  20  -5   4   3
  1  85  -5  34  21
  2  -5  22  31  75
  3  -5   5   7  -5

In your example you're just replacing the value of variable. You need to replace one cell's value using at method.

for i in df.iloc[:,df.columns.get_loc("T1"):df.columns.get_loc("T1")+4]<0:
for index, j in enumerate(df[i]):
    if j<0:
        df.at[index, i] = -5
like image 52
Mihai Alexandru-Ionut Avatar answered Oct 15 '22 21:10

Mihai Alexandru-Ionut


You can use indexing:

c = ['T1','T2','T3','T4']
df[df[c] < 0] = -5

Or clip:

In [47]: df[c].clip(lower=-5)
Out[47]:
   T1  T2  T3  T4
0  20  -5   4   3
1  85  -5  34  21
2  -5  22  31  75
3  -5   5   7  -5
like image 23
user3483203 Avatar answered Oct 15 '22 19:10

user3483203