0

If I have a table- similar to what you see below. How can I write a for loop that iterates between only columns 2-8 and would look at the input parameter and based on the condition specified in the for loop, replace the values in each row.

For example, if the input parameter is input=40, I would like to write a for loop that if it finds a value >= that input parameter in columns 2-8, then their values would be replaced by the input parameter. If the values are less than or equal to the input parameter, then the row values would remain the same.

If there are other methods to handle this, please feel free to share it. However, I want the output to populate all rows in specified columns.

enter image description here

2 Answers 2

0

I believe this should work. This should only change the columns in the columns list that are greater than or equal to the number variable. Hope this helps! (need to import numpy as np)

number = 5
columns = ['col1','col2']
df[columns] = np.where(df[columns]>=number,number,df[columns])
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. I think this should work, but I get the following error: "None of [Index(['2020-12-31', '2021-03-31'], dtype='object')] are in the [columns]"
Check to see if your columns have spaces on either side (or both sides). For example ' col1' instead of 'col1'
Did either of these solutions answer your question?
Yes, they did. Thank you.
Awesome! Please see link on how to close out a question. stackoverflow.com/help/someone-answers
0

Its similar to this answer

num=40

#where(condition, new value) replace the values where condition is False
df.loc[:, 'Col2':'Col8']= df.loc[:, 'Col2':'Col8'].where(df.loc[:, 'Col2':'Col8'] < num, num) 

However if you have column positions instead of column names you can replace df.loc[:, 'Col2':'Col8'] by df.iloc[:,[1,7]] which takes 2nd column to 8th column

Example

d={'Col1':[1,2,3,4,5,6,7],
    'Col2':[132, 93, 124, 475, 857, -46, 67],
    'Col3':[234, 123, 765, 1452, 542, 2562, 5876],
    'Col4':[7, 7 ,4 , 5 , 1, 9,3]}
data=pd.DataFrame(data=d)
print(data)
    Col1  Col2  Col3  Col4
0     1   132   234     7
1     2    93   123     7
2     3   124   765     4
3     4   475  1452     5
4     5   857   542     1
5     6   -46  2562     9
6     7    67  5876     3

replacing any values >=500 in Col2 to Col3 by 500

data.loc[:, 'Col2':'Col3']=data.loc[:, 'Col2':'Col3'].where(data.loc[:, 'Col2':'Col3'] < 500, 500)

After updating, replacing any values >=500 in Col2 to Col3 by 500

print(data)
    Col1  Col2  Col3  Col4
0     1   132   234     7
1     2    93   123     7
2     3   124   500     4
3     4   475   500     5
4     5   500   500     1
5     6   -46   500     9
6     7    67   500     3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.