[英]deleting rows based on a column's value in pandas
I have a dataframe like this: 我有这样的数据帧:
col.1 : a a a a a b b b c c c
col.2 : 0 0 1 0 0 0 1 0 0 0 0
I want to drop all values for a similar value in col.1 after 1 is encountered in col.2. 我想在col.2中遇到1之后删除col.1中类似值的所有值。 The result should be like:
结果应该是:
col.1 : a a a b b c c c
col.2 : 0 0 1 0 1 0 0 0
Is there any way to do it fast in pandas? 有没有办法在熊猫中快速做到这一点? Currently I'm using numpy where and it seems to be very slow.
目前我正在使用numpy,它似乎非常慢。
Try this: 尝试这个:
df['col.2'] = df.groupby('col.1')['col.2'].cumsum()
df['col.2'] = df.groupby('col.1')['col.2'].cumsum()
df = df[df['col.2']<2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.