Pandas에서 map, applymap, Apply 중에서 선택
Pandas DataFrames를 사용하다 보면 데이터에 함수를 적용해야 하는 경우가 많습니다. 다양한 방법으로. 벡터화에 일반적으로 사용되는 세 가지 방법은 map, applymap 및 apply입니다. 각각은 고유한 목적과 용도를 가지고 있습니다.
Map
map은 Series 객체에 특정한 방법이며 Series의 각 요소에 기능을 적용합니다. 단일 값을 입력으로 사용하고 단일 값을 반환하는 함수가 필요합니다.
예:
import pandas as pd # Create a Series series = pd.Series([1, 2, 3, 4, 5]) # Apply a function to each element def square(x): return x**2 # Apply the function to the series using map squared_series = series.map(square) print(squared_series)
출력:
0 1 1 4 2 9 3 16 4 25 dtype: int64
Applymap
applymap은 DataFrame의 각 요소에 함수를 적용하여 요소별로 작업을 수행합니다. map과 마찬가지로 단일 값을 입력으로 사용하고 단일 값을 반환하는 함수를 기대합니다.
예:
# Create a DataFrame df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}) # Apply a function to each element of the DataFrame def format_number(x): return "{:.2f}".format(x) # Apply the function to the DataFrame using applymap formatted_df = df.applymap(format_number) print(formatted_df)
출력:
a b 0 1.00 4.00 1 2.00 5.00 2 3.00 6.00
신청
신청 축 매개변수에 따라 DataFrame의 각 행 또는 열에 대한 함수입니다. map 및 applymap보다 더 다양하며 여러 값을 입력으로 전달해야 하는 함수를 처리할 수 있습니다.
예:
# Apply a function to each row of the DataFrame def get_max_min_diff(row): return row.max() - row.min() max_min_diff = df.apply(get_max_min_diff, axis=1) print(max_min_diff)
출력:
0 3.00 1 3.00 2 3.00 dtype: float64
사용법 요약
- map: 시리즈에 요소별 함수 적용
- applymap: DataFrame에 요소별 함수 적용
- 적용: 행/열별 함수 적용 유연한 입력/출력 처리를 통해 DataFrame으로
위 내용은 Pandas `map`, `applymap` 또는 `apply`를 언제 사용합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

forhandlinglargedatasetsinpython, usenumpyarraysforbetterperformance.1) numpyarraysarememory-effic andfasterfornumericaloperations.2) leveragevectorization foredtimecomplexity.4) managemoryusage withorfications data

inpython, listsusedyammoryAllocation과 함께 할당하고, whilempyarraysallocatefixedMemory.1) listsAllocatemememorythanneedInitiality.

Inpython, youcansspecthedatatypeyfelemeremodelerernspant.1) usenpynernrump.1) usenpynerp.dloatp.ploatm64, 포모 선례 전분자.

numpyissentialfornumericalcomputinginpythonduetoitsspeed, memory-efficiency 및 comperniveMathematicaticaltions

contiguousUousUousUlorAllocationScrucialForraysbecauseItAllowsOfficationAndFastElementAccess.1) ItenableSconstantTimeAccess, o (1), DuetodirectAddressCalculation.2) Itimprovesceeffiency theMultipleementFetchespercacheline.3) Itsimplififiesmomorym

slicepaythonlistisdoneusingthesyntaxlist [start : step : step] .here'showitworks : 1) startistheindexofthefirstelementtoinclude.2) stopistheindexofthefirstelemement.3) stepisincrementbetwetweentractionsoftortionsoflists

NumpyAllowsForVariousOperationsOnArrays : 1) BasicArithmeticLikeadDition, Subtraction, A 및 Division; 2) AdvancedOperationsSuchasmatrixmultiplication; 3) extrayintondsfordatamanipulation; 5) Ag

Arraysinpython, 특히 Stroughnumpyandpandas, areestentialfordataanalysis, setingspeedandefficiency


핫 AI 도구

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

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

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

Clothoff.io
AI 옷 제거제

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

인기 기사

뜨거운 도구

드림위버 CS6
시각적 웹 개발 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

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

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