0

I have a dataframe with a quantity column. How do I check which values in that column are integers?

I have tried

if df['quantity'].map(type) == int:
        True
else:
        item_no = df['item_number'].tolist()
        print(item_no)

which gives me the following ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Ideally, I would like the output to be a list of the item numbers where the quantity is not an integer.

onlywynter
  • 61
  • 7
  • `df['quantity'].map(type) == int` should return a Series of booleans, check it – mozway Jun 06 '22 at 17:44
  • 1
    `df['quantity'].map(type) == int` returns a series of `True` and `False` values. What does, say, `if [True, False, True, True]` mean? If you want to know if _all_ values are True, then do `if (df['quantity'].map(type) == int).all(): (....)`. If you want to know if _any_ is True, then use `.any()` and so forth. – rafaelc Jun 06 '22 at 17:44
  • Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Ynjxsjmh Jun 06 '22 at 17:51
  • @mozway that method still returned the same ValueError message as above – onlywynter Jun 06 '22 at 17:54
  • @rafaelc in your example, the False value is what i'm more interested in. i'd then want to see what value is there – onlywynter Jun 06 '22 at 17:57
  • @onlywynter then filter the dataframe. e.g. `df[df['quantity'].map(type) == int]` – rafaelc Jun 06 '22 at 17:59
  • Seems to me the big issue is that you're operating on the entire dataframe/column at once, when what youre trying to do is operate on each value in that column. You can iterate through the values with `df.iterrows()`, or you can take a look through the excellent [Pandas indexing and selecting data](https://pandas.pydata.org/docs/user_guide/indexing.html) docs for more options such as boolean indexing or `filter` or `query` – G. Anderson Jun 06 '22 at 18:03
  • There are many problems with the code shown here, and it isn't clear to me *what you expect the output to look like*. – Karl Knechtel Jun 06 '22 at 18:06

4 Answers4

0

If you want to check if there are any Python ints in your column, try this:

df['quantity'].map(type).eq(int).any()
0

Assuming you just want the indices where the value is an int, the following should suffice df.index[(df['quantity'].map(type)==int)].tolist()

Adib Nur
  • 89
  • 4
  • i tried 'q = df.index[(df['quantity'].map(type)==int)].tolist()' 'print(q)' but received only empty lists – onlywynter Jun 06 '22 at 18:11
  • There is a chance that the numbers in your data frame are not the standard python3 `int`, but rather something else, such as `numpy.int64` , etc. If you're sure that any particular index should be an `int` I would suggest just taking a look at what the `type(df['quantity][i])` for the ith index in this case is, and change the `int` in the above code to that type accordingly. Basically, I'm suggesting that you take a look at a few particular values manually in the data frame to see if they actually are `int`s. Also, maybe you could add some sample data to the question for clarification? – Adib Nur Jun 07 '22 at 19:34
0

If you want to identify the non integers you can use:

df[df['quantity'].map(type).ne(int)]

Or

df[~df['quantity'].apply(lambda x: isinstance(int, x))]

Or for non numbers:

df[pd.to_numeric(df['quantity'], errors='coerce').isna()]
mozway
  • 194,879
  • 13
  • 39
  • 75
0

The following code returns a list of the item numbers for where the 'quantity' column does not contain an integer

q = df.loc[~df['quantity'].str.isdigit(), 'item_num'].tolist()
print(q)
onlywynter
  • 61
  • 7