pandas .loc returns an empty data frame… here is a solution to the problem.
pandas .loc returns an empty data frame
I have a pandas data frame as shown below.
chainage(km)
0
0.001
0.002
0.003
0.004
When I search chainage(km)
using .loc, it returns an empty dataframe for some mileage.
print data.loc[data['chainage(km)'] == float(0.004)]
— Empty data frame
print data.loc[data['chainage(km)'] == float(0.001)]
— Return value
Any help would be appreciated.
Solution
The problem is caused by inaccurate floats. This is explained in Is floating point math broken?
In this case, use np.isclose instead.
df[np.isclose(data['chainage(km)'], 0.004)]