如何使用python查找包含特定名称的id



我有一个excel文件,包含如下所示的三列,

<表类> project_id 名称 日期 tbody><<tr>755war2019-04-08755Wabern2020-06-16755Wabern (FTTH geplan)2020-07-24755Wabern FTTH DTAG2020-08-15755Wabern (FTTH DTAG gg)2021-03-05755Wabern2021-09-13134Lerbeck2019-04-18134坏oyehausen FttH(圣)2020-06-26134Werre公园2020-07-14134Werre公园FTTH (ssd)2020-08-25134Werre公园(FTTH)2021-03-15134坏oyehausen2021-09-23584kitern2019-04-08584Lausen ftth (los)2020-06-16584kitener (FTTH geplan)2020-07-24584Lausern2020-08-15584Lausern (FTTH DTAG gg)2021-03-05

通过循环唯一id,每次通过特定id获得所有名称,在仅保留姓和名之后,将其转换为str并检查是否有单词'FTTH'

代码:

import numpy as np
[i for i in set(df.id.values) if 'FTTH' not in str(np.array(df[df['id']==i]['NM'])[[0,-1]])]

#[755, 134]

Using pandas:

def custom_function(series, pattern='FTTH'):
"""Identify if the first and last items do not have a pattern"""
first = pattern not in series.iat[0].upper()
last = pattern not in series.iat[-1].upper()
return first and last
df.groupby('project_id').Name.apply(custom_function)

输出:

project_id
134     True
584    False
755     True
Name: Name, dtype: bool

与pandas不同的方法:

res = df.groupby('project_id').apply(lambda x: ~x.Name.take([0,-1]).str
.contains('ftth',case=False).any())
res[res].reset_index().drop(0,axis=1)
>>>
'''
project_id
0   134
1   755

相关内容

  • 没有找到相关文章

最新更新