사전을 사용하여 Pandas 시리즈의 값 대체 성능 향상
사전을 사용하여 Pandas 시리즈의 값을 바꾸는 것은 일반적인 작업입니다. s.replace(d)를 사용하여 값을 바꾸는 것이 권장되지만 간단한 목록 이해를 사용하는 것보다 훨씬 느릴 수 있습니다.
성능 저하의 원인
느린 성능 s.replace(d)는 극단적인 경우와 드문 상황을 처리하는 데서 유래합니다. 여기에는 다음이 포함됩니다.
- 사전을 목록으로 변환
- 목록을 반복하고 중첩된 사전을 확인합니다.
- 키와 값의 반복자를 기능 교체.
대체 방법
성능을 향상하려면 다음 방법을 사용하는 것이 좋습니다.
- 전체 맵 : 계열의 모든 값이 사전에 의해 매핑되는 경우 s.map(d)를 사용하세요. 이 방법은 효율적이고 지속적으로 빠릅니다.
- 부분 맵: 값의 작은 부분(예: 5% 미만)만 사전에 매핑되는 경우 s.map(d ).fillna(s['A']).astype(int). 이 접근 방식은 매핑과 채우기를 결합하여 값비싼 반복 작업이 필요하지 않습니다.
벤치마킹
벤치마크는 s.replace(d)와 s 간의 성능 차이를 보여줍니다. .map(d) 및 목록 이해:
##### Full Map ##### d = {i: i+1 for i in range(1000)} %timeit df['A'].replace(d) # Slow (1.98s) %timeit df['A'].map(d) # Fast (84.3ms) ##### Partial Map ##### d = {i: i+1 for i in range(10)} %timeit df['A'].replace(d) # Intermediate (20.1ms) %timeit df['A'].map(d).fillna(df['A']).astype(int) # Faster (111ms)
이는 전체 또는 부분 매핑에서 s.map(d)가 s.replace(d)보다 일관되게 빠르다는 것을 보여줍니다.
결론
사전 적용 범위의 완전성에 따라 s.map(d) 또는 s.map(d).fillna(s['A']).astype(int) Pandas 시리즈에서 효율적인 값 교체를 위해서는 s.replace(d)보다 선호되어야 합니다.
위 내용은 Pandas 시리즈에서 사전을 사용하여 값을 바꾸는 것이 느린 이유는 무엇이며 성능을 어떻게 향상시킬 수 있습니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

Pythonusesahybridmodelofilationandlostretation : 1) ThePyThoninterPretreCeterCompileSsourcodeIntOplatform-IndependentBecode.

Pythonisbothingretedandcompiled.1) 1) it 'scompiledtobytecodeforportabilityacrossplatforms.2) thebytecodeisthentenningreted, withfordiNamictyTeNgreted, WhithItmayBowerShiledlanguges.

forloopsareusedwhendumberofitessiskNowninadvance, whilewhiloopsareusedwhentheationsdepernationsorarrays.2) whiloopsureatableforscenarioScontiLaspecOndCond

pythonisnotpurelynlogreted; itusesahybrideprophorfbyodecodecompilationandruntime -INGRETATION.1) pythoncompilessourcecodeintobytecode, thepythonVirtualMachine (pvm)

ToconcatenatelistsinpythonwithesameElements, 사용 : 1) OperatorTokeEpduplicates, 2) asettoremovedUplicates, or3) listComperensionForControlOverDuplicates, 각 methodHasDifferentPerferformanCeanDorderImpestications.

PythonisancerpretedLanguage, 비판적 요소를 제시하는 PytherfaceLockelimitationsIncriticalApplications.1) 해석 된 언어와 같은 thePeedBackandbackandrapidProtoTyping.2) CompilledlanguagesLikec/C transformt 해석

useforloopswhhenmerfiterationsiskNownInAdvance 및 WhileLoopSweHeniTesslationsDepoyConditionismet whilEroopsSuitsCenarioswhereTheLoopScenarioswhereTheLoopScenarioswhereTheLoopScenarioswhereTherInatismet, 유용한 광고 인 푸트 gorit


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

Dreamweaver Mac版
시각적 웹 개발 도구

맨티스BT
Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!