如何在 Python Pandas 中的两个值之间选择 DataFrame 中的行? [英] How to select rows in a DataFrame between two values, in Python Pandas?

查看:39
本文介绍了如何在 Python Pandas 中的两个值之间选择 DataFrame 中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改 DataFrame df 以仅包含 closure_price 列中的值介于 99 和 101 之间的行,并尝试使用代码执行此操作以下.

I am trying to modify a DataFrame df to only contain rows for which the values in the column closing_price are between 99 and 101 and trying to do this with the code below.

但是,我收到错误

ValueError:系列的真值不明确.使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

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

我想知道是否有办法在不使用循环的情况下做到这一点.

and I am wondering if there is a way to do this without using loops.

df = df[(99 <= df['closing_price'] <= 101)]

推荐答案

您应该使用 () 将布尔向量分组以消除歧义.

You should use () to group your boolean vector to remove ambiguity.

df = df[(df['closing_price'] >= 99) & (df['closing_price'] <= 101)]

这篇关于如何在 Python Pandas 中的两个值之间选择 DataFrame 中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆