我正在使用python 3.6来做到这一点。以下是我有兴趣查询的几个重要专栏。
Auto-Gen Index : Product Container : Ship Date :.......
0 : Large Box : 2017-01-09:.......
1 : Large Box : 2012-07-15:.......
2 : Small Box : 2012-07-18:.......
3 : Large Box : 2012-07-31:.......
我想查询指示Large Box作为其产品容器的行,并且发货日期必须在2012年7月期间。
file_name = r'''Sample-Superstore-Subset-Excel.xlsx'''
df = read_excel(file_name, sheet_name = my_sheet)
lb = df.loc[df['Product Container'] == 'Large Box'] //Get large box
july = lb[(lb['Ship Date'] > '2012-07-01') & (lb['Ship Date'] < '2012-07-31')]
我只是想知道如何使用python(pd.query())查询和where条件?
解决办法:想象一下loc过滤器 - 只给出符合条件的df部分。
where最初来自numpy。它遍历一个数组并检查每个元素是否符合条件。因此它会返回整个数组,结果或NaN。一个很好的特点是你也可以找回不同的东西,例如df2 = df.where(df ['Goals']> 10,other ='0'),用0替换不满足条件的值。
如果您要问何时使用query,AFAIK除了性能之外没有其他理由可做。如果您有一个非常大的数据集,则查询速度会更快。更多关于高级性能的信息。








暂无数据