热线电话:13121318867

登录
2019-03-11 阅读量: 741
从xlsx文件查询日期范围和产品大小

我正在使用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除了性能之外没有其他理由可做。如果您有一个非常大的数据集,则查询速度会更快。更多关于高级性能的信息。

8.7591
1
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子