다음과 같은 목록이 있다고 가정 해 보겠습니다.
certificates = [ISO9001, ISO203, CE2234]
다음과 같은 데이터 프레임 :
company_certificates
[ISO303, ISO9001]
[GlobalGAP12, ISO203]
[EuroGAP]
인증서 목록에 포함되지 않은 경우 company_certificates에서 요소를 제거하고 싶습니다. 다음과 같이 할 수 있습니다.
df['company_certificates'] = df['company_certificates'].apply(lambda x: [i for i in x if i in certificates])
최종 출력은 다음과 같습니다.
company_certificates
[ISO9001]
[ISO203]
[]
하지만 내 데이터 프레임이 거대하기 때문에 더 효율적인 방법이 필요합니다. 어떤 아이디어?
data = {'company_certificates': [['ISO303', 'ISO9001'], ['GlobalGAP12', 'ISO203'], ['EuroGAP']]}
data['company_certificates'] *= 1000000
df = pd.DataFrame(data)
certificates = ['ISO9001', 'ISO203', 'CE2234']
# 3.1 s ± 134 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
c2 = df['company_certificates'].tolist()
c1set = frozenset(certificates)
df['match'] = [[n for n in lst if n in c1set] for lst in c2]
# 4.32 s ± 578 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
df['match'] = df['company_certificates'].apply(lambda x: [i for i in x if i in certificates])
# 7.23 s ± 616 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
df['match'] = df['company_certificates'].apply(lambda x: list(set(x).intersection(certificates)))
# 9.43 s ± 913 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
df['match'] = df['company_certificates'].apply(lambda x: list(filter(lambda y: y in x, certificates)))
# 32 s ± 2.11 s per loop (mean ± std. dev. of 7 runs, 1 loop each)
mask = ~df['company_certificates'].explode().isin(certificates).reset_index() \
.groupby('index').any()['company_certificates']
df.loc[mask,'company_certificates'] = [[]*mask.sum()]
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다