6

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"]
Padfoot123
  • 1,057
  • 3
  • 24
  • 43

1 Answers1

14

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']
cs95
  • 379,657
  • 97
  • 704
  • 746
  • 1
    Works really well. – prosti May 21 '19 at 17:36
  • @prosti Thank you. I felt it was worth confirming the dtype of OP's data so we could find them a working solution quickly :) – cs95 May 21 '19 at 17:37
  • But it must be string = dtype('object') for that column since we deal with strings in that column. ;) else I am unaware something else may came in like dtype complex. – prosti May 21 '19 at 17:39
  • @prosti `object` is a broad sweeping generalisation for anything that isn't scalar. This includes strings as well as mixed types. If they are strings, you can directly find a solution. Otherwise you would need to convert them to string first to have the string methods actually return useful results on them. – cs95 May 21 '19 at 17:41
  • Thanks Guys.. I am getting error - bad operand type for unary ~: 'float' – Padfoot123 May 21 '19 at 17:43
  • Yes, these mixed types, I found one y-day :) I analyzed recently this: https://stackoverflow.com/questions/37561991/what-is-dtypeo/56189706#56189706 – prosti May 21 '19 at 17:43
  • @prosti That's a good answer but keep in mind that `object` can refer to literally anything that is not numeric or datetime. This also includes objects such as strings, lists, and even dictionaries. – cs95 May 21 '19 at 17:46
  • Yes. Its of data type - Object. How to find integers and then negate it for such mixed type columns? – Padfoot123 May 21 '19 at 17:47
  • @cs95 Will keep your tip. Tnx. – prosti May 21 '19 at 17:47