有没有办法用python检查某个值是否每周都出现?

dwbf0jvd  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(392)

我有一个数据集,上面有一个城市列表和每周的游客数量。我想做一个完整性检查,看看一个城市是否每周出现。
图中显示了数据集外观的快照,但实际数据集有近1563个城市:
[

我想检查一下,哈拉雷是否在这几周里一直出现,其他城市(1562个)也是如此。

x6h2sr28

x6h2sr281#

假设它存储在Pandas的Dataframe(名为df)中:

no_of_weeks = len(set(df['Week']))
check = pd.DataFrame(df.groupby('City').Week.apply(lambda x: x.count() == no_of_weeks))
always_recorded = pd.DataFrame({'City': check.index.values, 'always_recorded': check['Week']})
always_recorded.index = [*range(1, len(always_recorded)+1)]
df2 = pd.merge(df, always_recorded, how = "left", on = "City")
subset = df2[df2.always_recorded == True]
0aydgbwb

0aydgbwb2#

假设每个城市在给定日期只能出现一次,一种选择是使用关系划分技术:

select city
from mytable
group by city
having count(*) = (select count(distinct week) from mytable)

这将为您提供为所有日期显示的所有城市。
如果某个特定的城市在某个特定的日期出现不止一次,那么我们可以稍微改变一下 having 条款:

having count(distinct week) = (select count(distinct week) from mytable)
t3psigkw

t3psigkw3#

一个可能的解决方案是计算每个城市的周数,并将其与当前周数相匹配。
在sql中,可以执行以下操作

select 
   city, 
   count(week) over (partition by city) as appearance_num
from table_name

相关问题