根据工作日和周末 Pandas 选择数据
我有一个像这样的 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:
Any idea how I can fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 Pandas 很陌生,但我认为以下方法有效:
希望更有能力的人也可以帮助验证
I am new-ish to Pandas but I think the following works:
Hopefully someone more capable can also help to verify
为了解决这个问题,我使用 .loc 函数:
不是最快的解决方案,但它有效
For solving this I use the
.loc
function:Not the fastest solution, but it worked