Pandas の同等物: グループごとの個別カウント
データベースの代替として Pandas を使用する場合、多くの場合、個別のカウントなどの複雑な操作を実行する必要があります。グループ。この場合、年月ごとの個別のクライアントの数をカウントしたいと考えています。
SQL では、これは count(distinct) 集計関数を使用して実現できます。ただし、Pandas は、この操作に対して少し異なる構文を提供します。
Pandas で年月ごとに個別のクライアントをカウントするには、次のコードを使用できます。
<code class="python">table.groupby('YEARMONTH').CLIENTCODE.nunique()</code>
groupby() 関数指定された列 (この場合は YEARMONTH) に基づいて DataFrame をグループに分割します。次に、nunique() 関数は、各グループ内の一意の値の数をカウントします。
説明する例を次に示します。
<code class="python">import pandas as pd # Create a DataFrame with sample data data = { 'YEARMONTH': ['201301', '201301', '201301', '201302', '201302', '201302', '201302'], 'CLIENTCODE': [1, 1, 2, 1, 2, 2, 3] } table = pd.DataFrame(data) # Count distinct clients per year month result = table.groupby('YEARMONTH').CLIENTCODE.nunique() print(result)</code>
出力:
YEARMONTH 201301 2 201302 3
ご覧のとおり、結果は SQL クエリからの予想される出力と一致しています。
以上がPandas でグループごとに個別の値を数える方法は?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境
