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
To replace multiple values in a DataFrame, you can use DataFrame. replace() method with a dictionary of different replacements passed as argument.
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', ...])
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With