python - pandas drop rows after value appears
问题描述
I have a dataframe:
df = pd.DataFrame({'Position': [1,2,3,4,5,'Title','Name','copy','Thanks'], 'Winner': [0,0,0,0,0,'Johnson',0,0,0]})
I want to drop all the rows after and including the row Johnson appears in. This would give me a dataframe looking like:
df = pd.DataFrame({'Position': [1,2,3,4,5], 'Winner': [0,0,0,0,0]})
I have tried referencing the index that 'Johnson' appears in the slicing the dataframe using the index. But this didn't work for.
thanks
解决方案
你只需要布尔索引和cumsum
:
df[df.Winner.eq('Johnson').cumsum().lt(1)]
输出:
Position Winner
0 1 0
1 2 0
2 3 0
3 4 0
4 5 0
推荐阅读
- python - python 中的 os.listdir() 不显示目录的所有文件。它跳过了其中的几个
- c# - Xamarin Forms - 识别设备是否以纵向模式锁定
- javascript - 画布中对象之间的碰撞检查始终为正
- sql - Amazon Redshift:连接列内容略有不同的两列
- html - 禁用文本在 IE 和 Microsoft Edge 的网页上不起作用
- javascript - 如果文件上传未在 dropzone js 中完成,则禁用提交按钮
- mysql - 根据列值在一次查询中更新和删除
- windows-services - 使用 Robot Framework Process Library 终止服务
- django - 如何使用来自 django 的替代 json 格式将数据发送到动态 jstree
- mysql-8.0 - 在 mysql 8 中使用单个查询删除所有事件