繁体   English   中英

如何在 pandas dataframe 中的日期时间范围之间进行过滤

[英]How to filter between datetime range in pandas dataframe

我有一个 dataframe 我想根据day范围进行过滤并根据该条件设置month列。

简单来说,day(1-5) 之间的任何一天,从month减去 1,否则设置month = month

  day   month
0   1   6
1   4   6
2   4   6
3   4   6
4   4   6
5   4   6
6   6   6
7   8   6
8   12  6
9   12  6


if all(df[df['day'].between(1, 5)]):
    
    df.month = df.month - 1
    
else:
    df.month = df.month

预期 output

  day   month
0   1   5
1   4   5
2   4   5
3   4   5
4   4   5
5   5   5
6   6   6
7   8   6
8   12  6
9   12  6

您可以在'day'列上使用 Boolean 索引,并从'month'列的相关部分中减去 1:

df.loc[df.day<6, 'month'] -= 1

暂无
暂无

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

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