gpt4 book ai didi

python - 根据多个日期条件过滤 Dataframe

转载 作者:行者123 更新时间:2023-12-05 09:32:02 24 4
gpt4 key购买 nike

我正在使用以下 DataFrame:

id  slotTime    EDD EDD-10M
0 1000000101068957 2021-05-12 2021-12-26 2021-02-26
1 1000000100849718 2021-03-20 2021-04-05 2020-06-05
2 1000000100849718 2021-03-20 2021-04-05 2020-06-05
3 1000000100849718 2021-03-20 2021-04-05 2020-06-05
4 1000000100849718 2021-03-20 2021-04-05 2020-06-05

我只想保留 slotTime 所在的行介于 EDD-10M 之间和 EDD :

df['EDD-10M'] < df['slotTime'] < df['EDD']]

我尝试过使用以下方法:

df.loc[df[df['slotTime'] < df['EDD']] & df[df['EDD-10M'] < df['slotTime']]]

但是它会产生以下错误

TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

请指教。

要复制上述 DataFrame,请使用以下代码段:

import pandas as pd
from pandas import Timestamp

df = {
'id': {0: 1000000101068957,
1: 1000000100849718,
2: 1000000100849718,
3: 1000000100849718,
4: 1000000100849718,
5: 1000000100849718,
6: 1000000100849718,
7: 1000000100849718,
8: 1000000100849718,
9: 1000000100849718},
'EDD': {0: Timestamp('2021-12-26 00:00:00'),
1: Timestamp('2021-04-05 00:00:00'),
2: Timestamp('2021-04-05 00:00:00'),
3: Timestamp('2021-04-05 00:00:00'),
4: Timestamp('2021-04-05 00:00:00'),
5: Timestamp('2021-04-05 00:00:00'),
6: Timestamp('2021-04-05 00:00:00'),
7: Timestamp('2021-04-05 00:00:00'),
8: Timestamp('2021-04-05 00:00:00'),
9: Timestamp('2021-04-05 00:00:00')},
'EDD-10M': {0: Timestamp('2021-02-26 00:00:00'),
1: Timestamp('2020-06-05 00:00:00'),
2: Timestamp('2020-06-05 00:00:00'),
3: Timestamp('2020-06-05 00:00:00'),
4: Timestamp('2020-06-05 00:00:00'),
5: Timestamp('2020-06-05 00:00:00'),
6: Timestamp('2020-06-05 00:00:00'),
7: Timestamp('2020-06-05 00:00:00'),
8: Timestamp('2020-06-05 00:00:00'),
9: Timestamp('2020-06-05 00:00:00')},
'slotTime': {0: Timestamp('2021-05-12 00:00:00'),
1: Timestamp('2021-03-20 00:00:00'),
2: Timestamp('2021-03-20 00:00:00'),
3: Timestamp('2021-03-20 00:00:00'),
4: Timestamp('2021-03-20 00:00:00'),
5: Timestamp('2021-03-20 00:00:00'),
6: Timestamp('2021-03-20 00:00:00'),
7: Timestamp('2021-03-20 00:00:00'),
8: Timestamp('2021-03-20 00:00:00'),
9: Timestamp('2021-03-20 00:00:00')}}

df = pd.DataFrame(df)

最佳答案

你只需要把你的边分组

df[(df['slotTime'] < df['EDD']) & (df['EDD-10M'] < df['slotTime'])]

否则操作顺序首先尝试 & 事情,然后一切都崩溃了

或者,您可能希望使用 .between 运算符(假设您有一个日期时间系列

df[df['slotTime'].between(df['EDD'],df['EDD-10M'])]

关于python - 根据多个日期条件过滤 Dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68437046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com