使用两个条件索引 DataFrame
Index a DataFrame using two conditions
我正在尝试根据两个条件获取 DataFrame 的子集。
这是我的简化示例:
import pandas as pd
test = pd.DataFrame(np.ones(48),
index=pd.date_range('2015-01-01',
periods=48,
freq='1800S'))
我现在想要获取时间范围 t > 08:00 和 t < 22:00 内的所有值,因此我尝试了:
result = test[test.index.hour>8 & test.index.hour<22]
然后我得到 ValueError that the truth value of an array with more than one element is ambiguous, use a.any() or a.all()
- 在这里我运气不好...
您需要在使用元素方面之前将两个数组放在括号中 &
:
(test.index.hour > 8) & (test.index.hour < 22)
&
运算符比该表达式中的比较运算符具有 higher precedence,这导致了问题。
有 2 个简单的解决方案:
- 首先:将您的条件括在大括号中,例如
(test.index.hour > 8) & (test.index.hour<22)
由于运算符和优先级
- 第二:使用the query function
我正在尝试根据两个条件获取 DataFrame 的子集。
这是我的简化示例:
import pandas as pd
test = pd.DataFrame(np.ones(48),
index=pd.date_range('2015-01-01',
periods=48,
freq='1800S'))
我现在想要获取时间范围 t > 08:00 和 t < 22:00 内的所有值,因此我尝试了:
result = test[test.index.hour>8 & test.index.hour<22]
然后我得到 ValueError that the truth value of an array with more than one element is ambiguous, use a.any() or a.all()
- 在这里我运气不好...
您需要在使用元素方面之前将两个数组放在括号中 &
:
(test.index.hour > 8) & (test.index.hour < 22)
&
运算符比该表达式中的比较运算符具有 higher precedence,这导致了问题。
有 2 个简单的解决方案:
- 首先:将您的条件括在大括号中,例如
(test.index.hour > 8) & (test.index.hour<22)
由于运算符和优先级 - 第二:使用the query function