Pandas에서 Groupby를 사용한 전체 비율
주 내 각 사무실이 기여한 매출 비율을 계산하려면 Pandas에서 미묘한 접근 방식이 필요합니다. 단순히 '주' 및 'office_id'별로 그룹화하면 각 사무실의 매출 합계를 얻을 수 있지만 주 내 기여도 비율은 제공하지 않습니다.
이를 달성하려면 먼저 그룹별을 만들어야 합니다. 'state' 및 'office_id'를 기반으로 하는 개체입니다. 이렇게 하면 각 사무실-주 조합의 총 매출이 포함된 매출 열이 있는 데이터프레임이 제공됩니다.
state_office = df.groupby(['state', 'office_id']).agg({'sales': 'sum'})
백분율을 계산하려면 각 사무실의 매출을 해당 주의 총 매출로 나눌 수 있습니다. 그러나 groupby 내의 각 주의 총 매출에 액세스하려면 'state'를 기반으로 두 번째 groupby 개체를 생성해야 합니다.
state_total = df.groupby('state').agg({'sales': 'sum'})
이 개체를 사용하면 다음과 같이 'state_office' groupby를 향상할 수 있습니다. 각 사무실-주 조합에 대한 매출 비율을 포함하는 새 열:
state_pcts = state_office.groupby(level=0).apply(lambda x: 100 * x / float(state_total.loc[x.name]))
'groupby'의 'level=0' 매개변수는 원래 groupby가 'state' 및 'office_id'에 대해 형성한 다단계 지수의 최상위 수준입니다.
이러한 접근 방식을 사용하면 해당 주의 총 매출을 참조하여 각 사무실의 매출 비율을 계산할 수 있습니다.
위 내용은 Pandas를 사용하여 각 주 내 사무실당 매출 비율을 계산하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 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 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

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

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

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경