删除基于有效数据百分比的 Pandas 行

huangapple go评论56阅读模式
英文:

Drop pandas rows based on percentage of valid data

问题

我有一个类似这样的pandas数据帧

Date_Time level
2018-02-12 13:22:27 5
2018-02-12 13:17:27 7
2018-02-12 13:12:27 2
2018-02-12 13:07:27 6
2018-02-13 13:12:27 4
2018-02-13 13:17:27 5

如何使特定日期的条目少于3个时将其删除,即自2018-03-13起,删除<4个条目,并获取此表

Date_Time level
2018-02-12 13:22:27 5
2018-02-12 13:17:27 7
2018-02-12 13:12:27 2
2018-02-12 13:07:27 6

我尝试使用for循环,但运行时间太长。

英文:

I have a pandas data frame that looks like this

Date_Time level
2018-02-12 13:22:27 5
2018-02-12 13:17:27 7
2018-02-12 13:12:27 2
2018-02-12 13:07:27 6
2018-02-13 13:12:27 4
2018-02-13 13:17:27 5

How do I make it so If there is less than 3 entries on a specific date they get removed
i.e since 2018-03-13 < 4 entries remove them and get this table

Date_Time level
2018-02-12 13:22:27 5
2018-02-12 13:17:27 7
2018-02-12 13:12:27 2
2018-02-12 13:07:27 6

I tried using a for loop but that takes too long to run

答案1

得分: 0

你可以使用 groupbytransform 来进行 count 操作,然后使用 ge 来获取你想要的行:

df[df.groupby(df['Date_Time'].dt.date)['Date_Time'].transform('count').ge(4)]
英文:

You can do groupby and transform with count and then use ge to get the rows you wanted:

df[df.groupby(df[&#39;Date_Time&#39;].dt.date)[&#39;Date_Time&#39;].transform(&#39;count&#39;).ge(4)]

huangapple
  • 本文由 发表于 2023年2月6日 09:50:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定