I have a pandas dataframe with a column which could have integers, float, string etc. I would like to iterate over all the rows and check if each value is integer and if not, I would like to create a list with error values (values that are not integer)
I have tried isnumeric(), but couldnt iterate over each row and write errors to output. I tried using iterrows() but it converts all values to float.
ID Field1
1 1.15
2 2
3 1
4 25
5 and
Expected Result:
[1.15,"and"]
If "Field1" is a column of strings, use str.isdigit
(returns True for integers only) and negate:
df.loc[~df['Field1'].str.isdigit(), 'Field1'].tolist()
# ['1.15', 'and']
Alternatively, if the column contains mixed types, use
df.loc[~df['Field1'].astype(str).str.isdigit(), 'Field1'].tolist()
# [1.15, 'and']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With