When the pandas read_csv()
function loads the CSV file into a dataframe, it will assign the float dtype to any column that contains float and integer values. To test if the elements of the column can be expressed exactly as integers, you can use the .is_integer()
method of floats as described in the answer to How to check if float pandas column contains only integer numbers?
In your case, you want to verify that you have only one float in the column, so do this:
import pandas as pd
df = pd.DataFrame({'name':[f"random{i}" for i in range(1,6)], 'num':[2, 3, 2.89, 1, 3.45]})
if sum(~df.num.apply(float.is_integer)) != 1:
print("Error, the data column contains the wrong number of floats!")
If it is possible that the column only contains integers, then the column will have an integer dtype and the above code will cause an error. You could catch the error, or you could also test for this case:
from pandas.api.types import is_float_dtype
if not is_float_dtype(df.num) or sum(~df.num.apply(float.is_integer)) != 1:
print("Error, the data column contains the wrong number of floats!")