根据工作日和周末 Pandas 选择数据

发布于 2025-01-11 09:20:23 字数 1309 浏览 0 评论 0原文

我有一个像这样的 df:

import pandas as pd
import numpy as np

datetime = [('2019-09-15 00:15:00.000000000'),
            ('2019-09-15 00:30:00.000000000'),
            ('2019-09-15 00:45:00.000000000'),
            ('2019-09-15 01:00:00.000000000'),
            ('2019-09-15 01:15:00.000000000'),
            ('2019-09-15 01:30:00.000000000'),
            ('2019-09-15 01:45:00.000000000'),
            ('2019-09-15 02:00:00.000000000'),
            ('2019-09-15 02:15:00.000000000')]
p =[494.76,486.36,484.68,500.64,482.16,483.84,483.0,478.8,493.08,474.6]
q = [47.88,33.6,41.16,0.0,0.0,0.0,0.0,0.0,0.0,0.0]

df = pd.DataFrame(zip(datetime, p, q), columns=['datetime','p','q'])
df['week'] (df['datetime'].astype('datetime64[ns]') + pd.Timedelta(seconds=-15*60)).dt.day_name()

现在我已经定义了一周中的哪几天,每天从 00:15:00 开始,到 00:00:00 结束> 96分后。我想将它们分成另外两个 df。一个只有工作日,另一个只有周末。我已经尝试过:

df_week = df[~df['datetime'].dt.day_name().isin(['Saturday','Sunday'])]
df_weekend = df[df['datetime'].dt.day_name().isin(['Saturday','Sunday'])] 

虽然代码运行没有错误,但我的输出包含 df_week 的“星期六”的一个数据。像这样:

“在此处输入图像描述”

知道如何解决此问题吗?

I have a df like this:

import pandas as pd
import numpy as np

datetime = [('2019-09-15 00:15:00.000000000'),
            ('2019-09-15 00:30:00.000000000'),
            ('2019-09-15 00:45:00.000000000'),
            ('2019-09-15 01:00:00.000000000'),
            ('2019-09-15 01:15:00.000000000'),
            ('2019-09-15 01:30:00.000000000'),
            ('2019-09-15 01:45:00.000000000'),
            ('2019-09-15 02:00:00.000000000'),
            ('2019-09-15 02:15:00.000000000')]
p =[494.76,486.36,484.68,500.64,482.16,483.84,483.0,478.8,493.08,474.6]
q = [47.88,33.6,41.16,0.0,0.0,0.0,0.0,0.0,0.0,0.0]

df = pd.DataFrame(zip(datetime, p, q), columns=['datetime','p','q'])
df['week'] (df['datetime'].astype('datetime64[ns]') + pd.Timedelta(seconds=-15*60)).dt.day_name()

And now that I have defined the days of the week where each day starts at 00:15:00 and ends at the 00:00:00 after 96 points. I want to separate them into two other df's. In one only the weekdays and in the other only the weekends. I've tried:

df_week = df[~df['datetime'].dt.day_name().isin(['Saturday','Sunday'])]
df_weekend = df[df['datetime'].dt.day_name().isin(['Saturday','Sunday'])] 

Although the code runs without errors, I'm having an output that includes one data for "Saturday" at the df_week. Like this:

enter image description here

Any idea how I can fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捶死心动 2025-01-18 09:20:23

我对 Pandas 很陌生,但我认为以下方法有效:

df1 = df[df['week'].isin(['Sunday', 'Saturday'])]

df2 = df[~df['week'].isin(['Sunday', 'Saturday'])]

希望更有能力的人也可以帮助验证

I am new-ish to Pandas but I think the following works:

df1 = df[df['week'].isin(['Sunday', 'Saturday'])]

df2 = df[~df['week'].isin(['Sunday', 'Saturday'])]

Hopefully someone more capable can also help to verify

稀香 2025-01-18 09:20:23

为了解决这个问题,我使用 .loc 函数:

monday = df.loc[(df['week'] == 'Monday')]
tuesday = df.loc[(df['week'] == 'Tuesday')]
df_w = pd.concat([monday,tuesday])

不是最快的解决方案,但它有效

For solving this I use the .loc function:

monday = df.loc[(df['week'] == 'Monday')]
tuesday = df.loc[(df['week'] == 'Tuesday')]
df_w = pd.concat([monday,tuesday])

Not the fastest solution, but it worked

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文