在“lid”列中有一个重复值的数据框。我想使用Pandas删除列“lid”中其值被计数超过2次的行。 这是原始表格:
entity pnb head# state lid
ABB001 A03 3 DOWN A
ABB001 A03 3 DOWN A
ABB001 A03 3 DOWN A
ABB002 A02 4 DOWN B
ABB002 A02 4 DOWN B
ABB002 A02 2 DOWN C
ABB002 A02 4 DOWN D
ABB002 A02 4 DOWN E
ABB002 A02 4 DOWN E
ABB002 A02 4 DOWN E
结果如下:
entity pnb head# state lid
ABB002 A02 4 DOWN B
ABB002 A02 4 DOWN B
ABB002 A02 2 DOWN C
ABB002 A02 4 DOWN D
将^{} 与^{} 一起使用:
细节:
另一个具有filter的更慢的解决方案:
时间安排:
设置:
警告
结果并不涉及给定组数的性能,这将在很大程度上影响某些解决方案的计时。你知道吗
使用
groupby
+transform
。你知道吗transform
获取一系列相同大小的计数。你知道吗用它来找出需要去哪一行。你知道吗
使用掩码索引
df
。你知道吗选项0
使用
value_counts
和isin
选项1
更好地用
np.in1d
和pd.factorize
实现选项2
使用
np.bincount
和pd.factorize
为了有趣的演示来强调@cᴏʟᴅsᴘᴇᴇᴅ和我在评论中谈论的内容。你知道吗
时间测试
相关问题 更多 >
编程相关推荐