如何在Python Pandas中的两个值之间选择DataFrame中的行? [英] How to select rows in a DataFrame between two values, in Python Pandas?
本文介绍了如何在Python Pandas中的两个值之间选择DataFrame中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将DataFrame df
修改为仅包含列closing_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屋!
查看全文