检查列中的值是否为整数并返回true

发布于 2025-02-05 02:22:10 字数 342 浏览 1 评论 0原文

我有一个带有数量列的数据框。如何检查该列中的哪些值是整数?

我尝试过

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

,这给了我以下valueerror:系列的真实价值是模棱两可的。使用A.Empty,A.Bool(),A.Item(),A.Any()或A.all()。

理想情况下,我希望输出成为项目编号的列表数量不是整数。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笙痞 2025-02-12 02:22:10

如果要检查列中是否有python int s,请尝试以下操作:

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

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

df['quantity'].map(type).eq(int).any()
世界和平 2025-02-12 02:22:10

假设您只需要值的索引,则以下应该足够df.index [(df [Quantity']。map(type)== int)]。tolist(tolist()

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

逆流 2025-02-12 02:22:10

如果要识别可以使用的非整数:

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

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

用于非数字:

df[pd.to_numeric(df['quantity'], errors='coerce').isna()]

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()]
调妓 2025-02-12 02:22:10

以下代码返回“数量”列不包含整数的项目编号列表

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文