Pandas のリストの列: 各リスト要素の行の作成
Pandas データフレームでは、リストを含む列を変換する必要がある場合があります。複数の行に分割され、リストの各要素が独自の行を占めます。これを実現するには、2 つの実行可能なオプションがあります。
Pandas>=0.25 の組み込みの Explode メソッド
Pandas バージョン 0.25 以降の場合、.explode( ) メソッドは、この目的のために特別に導入されました。リストを個々の行に効率的に変換します。
df = pd.DataFrame({'trial_num': [1, 2, 3, 1, 2, 3], 'subject': [1, 1, 1, 2, 2, 2], 'samples': [list(np.random.randn(3).round(2)) for i in range(6)] }) df.explode('samples').reset_index(drop=True) # Resetting the index for clarity
このメソッドは、リストとスカラー、および空のリストと NaN を含む混合列を処理します。ただし、Explore は一度に 1 つの列に対して動作することに注意することが重要です。
Pandas のカスタム関数
Pandas の以前のバージョンの場合、カスタム関数を利用できます:
def explode_list_column(df, column): # Create an empty list to store the expanded rows exploded_rows = [] # Iterate through each cell in the specified column for row in df[column]: # Iterate through each element in the list for element in row: # Add a new row to the list, copying data from the current row and adding the new element exploded_rows.append(list(row) + [element]) # Convert the expanded rows into a DataFrame return pd.DataFrame(exploded_rows, columns=df.columns + ['list_element'])
この関数は、DataFrame とリストを含む列の名前を受け取ります。パラメータを取得すると、各リスト要素の列を含む新しい DataFrame が返されます。
# Applying the exploding function exploded_df = explode_list_column(df, 'samples')
以上がリストの 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 中国語版
中国語版、とても使いやすい

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

WebStorm Mac版
便利なJavaScript開発ツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。
