我有一个这样的数据框:
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
在 col.2 中遇到 1 后,我想删除 col.1 中相似值的所有值。结果应该是这样的:
col.1 : a a a b b c c c
col.2 : 0 0 1 0 1 0 0 0
有什么方法可以在 pandas 中快速完成吗?目前我在哪里使用 numpy,它似乎很慢。
最佳答案
试试这个:
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]
关于python - 根据 Pandas 中列的值删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23245905/