Pandas の複数の列の値に基づいて新しい列を作成する
複数の列の値に基づいて Pandas データフレームに新しい列を作成するには他の列では、apply() 関数を利用できます。この関数を使用すると、データフレームの各行にカスタム関数を適用できます。
この場合、次の基準に基づいて人種ラベルを含む新しい列を作成します:
人種ラベル基準:
- ERI_Hispanic 列が 1 の場合、ラベルは"ヒスパニック"
- それ以外の場合、残りの ERI 列の合計が 1 より大きい場合、ラベルは "Two or More" になります。
- それ以外の場合、ERI_AmerInd_AKNatv 列が 1 の場合、ラベルは「A/I AK ネイティブ。」
- ERI_Asian 列が1、ラベルは「Asian」です。
- それ以外の場合、ERI_Black_Afr.Amer 列が 1 の場合、ラベルは「Black/AA」です。
- それ以外の場合、ERI_HI_PacIsl 列が 1 の場合、ラベルはは "Haw/Pac Isl."
- Else if ERI_White 列は 1、ラベルは「White」です。
レース ラベリングのカスタム関数:
レース ラベリングのカスタム関数を定義するには、次のようにします。次のコードを使用します:
def label_race(row): if row['ERI_Hispanic'] == 1: return 'Hispanic' if row['ERI_AmerInd_AKNatv'] + row['ERI_Asian'] + row['ERI_Black_Afr.Amer'] + row['ERI_HI_PacIsl'] + row['ERI_White'] > 1: return 'Two Or More' if row['ERI_AmerInd_AKNatv'] == 1: return 'A/I AK Native' if row['ERI_Asian'] == 1: return 'Asian' if row['ERI_Black_Afr.Amer'] == 1: return 'Black/AA' if row['ERI_HI_PacIsl'] == 1: return 'Haw/Pac Isl.' if row['ERI_White'] == 1: return 'White' return 'Other'
カスタム関数を適用するapply():
label_race 関数をデータフレームの各行に適用するには、関数を各行に適用することを指定する axis=1 引数を指定して apply() 関数を使用できます。 row:
df['race_label'] = df.apply(label_race, axis=1)
これにより、適切なレース ラベルを含む、race_label という名前の新しい列がデータフレームに作成されます。
以上が複数の既存の列に基づいて Pandas で新しい Race Label 列を作成する方法の詳細内容です。詳細については、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 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

Dreamweaver Mac版
ビジュアル Web 開発ツール

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

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

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

ホットトピック









