Skip to content Skip to sidebar Skip to footer

Getting The Index Of A Float In A Column Using Pandas

I have a dataset that I am pulling using pandas. It looks like this: import pandas as pd dataset=pd.read_csv('D:\\filename.csv', header=None, usecols=3,4,10,16,22,28]) time=dataset

Solution 1:

Assuming you're dealing with floats, you can't use an equality comparison here (because of floating point inaccuracies creeping in).

Use np.isclose + np.argmax:

idx = np.isclose(df['time'], 0.00017).argmax()

If there's a possibility this value may not exist:

m = np.isclose(df['time'], 0.00017)
if m.sum() > 0:
    idx = m.argmax()

Otherwise, set idx to whatever (None, -1, etc).

Post a Comment for "Getting The Index Of A Float In A Column Using Pandas"