我正在使用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条件?
如果你的问题是什么时候使用qazxsw poi vs qazxsw poi,请参阅我的回答qazxsw poi:
把
loc
想象成一个过滤器 - 只给我符合条件的df部分。
where
最初来自numpy。它遍历一个数组并检查每个元素是否符合条件。因此它会返回整个数组,结果或NaN。一个很好的特点是你还可以找到不同的东西,例如: df2 = df.where(df ['Goals']> 10,other ='0'),用0替换不满足条件的值。
如果你问什么时候使用here,AFAIK除了性能之外没有其他真正的理由。如果您有一个非常大的数据集,则查询速度会更快。更多关于高级性能loc
。