2015-10-06 47 views
3

假设我们在Pandas中有一个数据帧df,有什么方法可以定义query表达式,以便我仍然可以获取数据帧的完整?返回完整数据帧的查询表达式

万一有帮助,我使用的使用的方式类似RO:

if cond: 
    condition = 'some expression' 
else: 
    condition = '<return everything>' 


df.query(condition) 

我想:

  • df.query('True')
  • df.query(True)

没有运气。

也,df.query(1)似乎只检索一条记录。

回答

3

这是一个有些奇怪,但我想你可以做index == index or index != index

>>> df = pd.DataFrame({"A": [1,2,np.nan]}, index=[10,20,np.nan]) 
>>> df 
     A 
10 1 
20 2 
NaN NaN 
>>> df.query("index == index or index != index") 
     A 
10 1 
20 2 
NaN NaN 

index != index分支处理的NaN的情况。

+0

'df.query(“index”)'也适用于这个例子。 –

+0

@FabianRost:true,但不幸的是,它不适用于对象dtype索引。不过,我可能会比其他人更多地使用它们。 – DSM