Skip to content
Advertisement

How can I make a: for i in range work with a float [closed]

So I was working on this project but I ran stuck (again), I have a excel file with 344 columns and he should check if a cell has a certain and if it has then it should continue and print a word. If it doesnt then it should also continue but then without printing. I believe that I have the right code but it still returns an error.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pygef.gef import ParseGEF

#Read *.gef file
gef = ParseGEF("./CPT000000052185_IMBRO_A.Gef")
gef.df.to_csv("./CPT000000052185_IMBRO_A.GEF")
file = pd.read_csv("./CPT000000052185_IMBRO_A.GEF", usecols=['friction_number' , 'depth'])
print (file)

FrictionNumber = file.friction_number
print (FrictionNumber)
FrictionNumber.dtypes

for i in FrictionNumber:
    if (0.2 < FrictionNumber [i] < 2.0):
        print ("Gravel")

This is the code, and it returns this error:

cannot do label indexing on <class 'pandas.core.indexes.range.RangeIndex'> with these indexers [1.4986376021798364] of <class 'float'>
``` (1.4986... is the value of the first cell)

If I’m right it returns this because the value in the cell is a float. How can I make this work with a float?

Advertisement

Answer

You already was iterating over FrictionNumber with i, so you could just directly use the value i for comparing.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pygef.gef import ParseGEF

#Read *.gef file
gef = ParseGEF("./CPT000000052185_IMBRO_A.Gef")
gef.df.to_csv("./CPT000000052185_IMBRO_A.GEF")
file = pd.read_csv("./CPT000000052185_IMBRO_A.GEF", usecols=['friction_number' , 'depth'])
print (file)

FrictionNumber = file.friction_number
print (FrictionNumber)
FrictionNumber.dtypes

for i in FrictionNumber:
    if (0.2 < i < 2.0):
        print ("Gravel")
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement