你好,我有一个数据帧,比如
col1 col2
G1 OP2
G1 OP0
G1 OPP
G1 OPL_Lh
G2 OII
G2 OIP
G2 IOP
G3 TYU
G4 TUI
G4 TYUI
G4 TR_Lh
我想通过groupby并从df-tha组中删除,这些组在col2中不包含至少一行包含
'_Lh'
在这里,我应该只保留G1
和G4
并获得:
col1 col2
G1 OP2
G1 OP0
G1 OPP
G1 OPL_Lh
G4 TUI
G4 TYUI
G4 TR_Lh
有人有主意吗?多谢各位
Tags:
解决这个问题还有很长的路要走,来说明
groupby
是如何工作的首先创建一个函数,用于测试所需字符串:
接下来,迭代您的组并应用此函数:
最后,将该词典映射到您当前的df:
您可以使用以下代码压缩这些步骤:
你可以做:
产出:
IIUC
您可以使用布尔测试和
isin
在包含_Lh
的组中进行筛选相关问题 更多 >
编程相关推荐