删除 pandas 数据框中具有相同值的连续行
如何仅删除 pandas 数据框中具有相同值的三个连续行(在下面的示例中,这将是整数“4”)。
考虑以下代码:
import pandas as pd
df = pd.DataFrame({
'rating': [4, 4, 3.5, 15, 5 ,4,4,4,4,4 ]
})
rating
0 4.0
1 4.0
2 3.5
3 15.0
4 5.0
5 4.0
6 4.0
7 4.0
8 4.0
9 4.0
我希望获得以下结果作为输出,其中包含被删除的值“4”的三个连续行:
0 4.0
1 4.0
2 3.5
3 15.0
4 5.0
5 4.0
6 4.0
How can I delete only the three consecutive rows in a pandas dataframe that have the same value (in the example below, this would be the integer "4").
Consider the following code:
import pandas as pd
df = pd.DataFrame({
'rating': [4, 4, 3.5, 15, 5 ,4,4,4,4,4 ]
})
rating
0 4.0
1 4.0
2 3.5
3 15.0
4 5.0
5 4.0
6 4.0
7 4.0
8 4.0
9 4.0
I would like to get the following result as output with the three consecutive rows containing the value "4" being removed:
0 4.0
1 4.0
2 3.5
3 15.0
4 5.0
5 4.0
6 4.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次存在新值时首先获取一个组,然后使用
GroupBy.head
first get a group each time a new value exists, then use
GroupBy.head
使用
GroupBy。 cumcount
用于布尔索引
:Use
GroupBy.cumcount
for counter and filter in rows inboolean indexing
: