简体   繁体   English

根据范围内日期列的月份从 Pandas DataFrame 检索行

[英]Retrieving rows from Pandas DataFrame based on month of a date column in a range

I currently have a table called Sales .我目前有一个名为Sales的表。 The Sales table has a column called sale_date which is in the form YYYY-MM-DD and I want to extract rows where the month is within a range. Sales表有一个名为sale_date的列,其格式YYYY-MM-DD ,我想提取月份在一个范围内的行。

| seller_id | product_id | buyer_id | sale_date    | quantity | price |
|-----------|------------|----------|--------------|----------|-------|
| 7         | 11         | 49       | '2019-01-21' | 5        | 3330  |
| 13        | 32         | 6        | '2019-02-10' | 9        | 1089  |
| 50        | 47         | 4        | '2019-01-06' | 1        | 1343  |

I've tried something like:我试过类似的东西:

>>> df.loc[df['sale_date'].str.split('-').isin([1, 2, 3])]
>>> df.loc[[int(x[1]) for x in df['sale_date'].str.split('-')][1] in [1, 2, 3]]

but these result in a type error and key error, respectively.但是这些分别导致类型错误和键错误。

Is there any way that I can extract just the month from the sale_date column and check whether it's in a range?有什么方法可以从sale_date列中提取月份并检查它是否在一个范围内? Thanks.谢谢。

You can convert values to datetimes and then extract months:您可以将值转换为日期时间,然后提取月份:

df.loc[pd.to_datetime(df['sale_date']).dt.month.isin([1, 2, 3])]

Or modify your solution with extract second values from list by indexing str[1] with casting to integers:或者通过索引str[1]并转换为整数来修改您的解决方案,从列表中提取第二个值:

df.loc[df['sale_date'].str.split('-').str[1].astype(int).isin([1, 2, 3])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM