Pandas でグループ化された DataFrame に列を追加する
Pandas でデータフレームを操作する場合、多くの場合、データをグループ化し、データフレーム内の値を集計する必要があります。グループ。通常、これには、集計結果を含む別のデータフレームの作成が含まれます。ただし、これらの集計列を元のデータフレームに直接追加すると便利な場合もあります。
簡単なデータフレームの例でこれを説明してみましょう。
<code class="python">df = pd.DataFrame({'c': [1, 1, 1, 2, 2, 2, 2], 'type': ['m', 'n', 'o', 'm', 'm', 'n', 'n']})</code>
「type」の値をカウントするには「c」の各値に対して、次のコードを使用できます。
<code class="python">g = df.groupby('c')['type'].value_counts().reset_index(name='t')</code>
これにより、3 つの列を持つ新しいデータフレーム 'g' が作成されます: 'c'、'type'、および 't' は、各 'c' 内の各 'type' の数。
次に、'size()' メソッドを使用して各グループ内の行数を数えます。
<code class="python">a = df.groupby('c').size().reset_index(name='size')</code>
これ2 つの列を持つ新しいデータフレーム 'a' を作成します: 'c' と、各 'c' グループの行数を含む 'size'。
元のデータフレームに 'size' 列を追加するには、1 つのオプションがあります。質問に示されているように、「map()」関数を使用することです。ただし、より簡単なアプローチは、「transform()」メソッドを使用することです。
<code class="python">g['size'] = df.groupby('c')['type'].transform('size')</code>
「transform()」メソッドは、元のデータフレームに合わせたインデックスを持つ Series を返します。このシリーズをグループ化されたデータフレームの新しい列に割り当てることで、集計された値を元のデータフレームに効果的に追加します。
結果のデータフレーム 'g' には、追加の 'size' 列が含まれるようになります。
c type t size 0 1 m 1 3 1 1 n 1 3 2 1 o 1 3 3 2 m 2 4 4 2 n 2 4
このアプローチは、Pandas のグループ化されたデータフレームに集計列を追加する簡単かつ効率的な方法を提供します。
以上がPandas でグループ化された DataFrame に集計列を追加する方法の詳細内容です。詳細については、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 統合開発環境
