[英]Python Dataframes: Count how many of occurrences of value on specific date and delete based on condition
I am using python and pandas for my assignment.我正在使用 python 和 pandas 来完成我的任务。 my Datafrmae looks something like this:
我的 Datafrmae 看起来像这样:
Date![]() |
Time![]() |
Business hours![]() |
---|---|---|
2021/4/26 ![]() |
0800 ![]() |
NO![]() |
2021/4/26 ![]() |
0900 ![]() |
Yes![]() |
2021/4/26 ![]() |
1000 ![]() |
Yes![]() |
I want to figure out if the date was a holiday by counting how many yes and no there are on a specific date - if the count of yes is less then 7, then I would deem that date a Holiday and exclude it from my calculations by deleting it.我想通过计算特定日期有多少是和否来确定日期是否是假期 - 如果是的计数小于 7,那么我会认为该日期是假期并将其从我的计算中排除删除它。
I was thinking of adding a holiday column with boolean value.我正在考虑添加一个带有 boolean 值的假期列。 Been looking all over for a solution online but falling short.
一直在网上寻找解决方案,但功亏一篑。 Im pretty new to Python so i apologise if i said anything stupid
我对 Python 很陌生,所以如果我说了什么愚蠢的话,我深表歉意
We can use transform here with groupby:我们可以在这里使用带有 groupby 的转换:
s = df["Business hours"].eq("Yes").groupby(df["Date"]).transform("Sum")
df[s >= 7]
Try groupby filter function:尝试 groupby 过滤器 function:
def filter_rows(x):
try:
x['Business hours'].value_counts()['Yes'] >= 7
return True
except KeyError as e:
return False
df = df.groupby('Date').filter(filter_rows)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.