0

I have two list, an empty list(newgr) and the other got values that has been imported from an excel sheet(gr). I'm trying to iterate through the list in a for-loop so that I can compare and change some of the values and append the values in gr that have changed with the original values into newgr. I tried this format but it wasn't working.

df = pd.read_excel("Well_Logs.xlsx", "SB16", na_values = [-999])
newgr = []
gr = df['GR'].values.tolist()

grclean = int(input('enter your clean value >')) #20 for example
grclay = int(input('enter your clay value >')) #60 for example

for i in gr:

    if i < grclean:
        i==grclean
        newgr.append(i)
    elif i > grclay:
        i==grclay
        newgr.append(i)
    else:
        newgr.append(i)

print(newgr)

I'm expecting the list to have some new values to work with, rather the same values of gr is appended to newgr.

2
  • Just for clarification, when you print newgr, you only get the values that are in gr right? Commented Apr 10, 2019 at 13:55
  • Yes. I'm expecting to some of the values to change. Commented Apr 10, 2019 at 21:18

2 Answers 2

2

You should use

i = grclean

to assign grclean to i

instead of

i == grclean

which is a comparison.

You can also write the entire loop as a neat list comprehension:

newgr = [max(grclean, min(grclay, i)) for i in gr]
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this code on its own and it worked but it gave me an error when I ran it as part of my script.
'<' not supported between instances of 'float' and 'NoneType'
Your list contains Nones then.
0
df = pd.read_excel("Well_Logs.xlsx", "SB16", na_values = [-999])
newgr = []
gr = df['GR'].values.tolist()

grclean = int(input('enter your clean value >')) #20 for example
grclay = int(input('enter your clay value >')) #60 for example

for i in gr:

    if i < grclean:
        newgr.append(grclean)
    elif i > grclay:
        newgr.append(grclay)
    else:
        newgr.append(i)

print(newgr)

1 Comment

Thanks. Makes sense

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.