ピボットのための DataFrame の転置
DataFrame のピボットとは、データの異なる表現を実現するために行と列を転置することを指します。 Pandas では、この目的のためにピボット メソッドを使用できます。
次の DataFrame を検討します:
Indicator Country Year Value 1 Angola 2005 6 2 Angola 2005 13 3 Angola 2005 10 4 Angola 2005 11 5 Angola 2005 5 1 Angola 2006 3 2 Angola 2006 2 3 Angola 2006 7 4 Angola 2006 3 5 Angola 2006 6
この DataFrame をピボットしてインジケーター値を新しい列として持つには:
out = df.pivot(columns = 'Country', 'Year', 'Indicator', values = 'Value') print(out)
これにより次の結果が得られます:
Indicator 1 2 3 4 5 Country Year Angola 2005 6 13 10 11 5 2006 3 2 7 3 6
アンピボット形式に戻すには:
print(out.rename_axis(columns=None).reset_index())
これにより次の結果が得られます:
Country Year 1 2 3 4 5 0 Angola 2005 6 13 10 11 5 1 Angola 2006 3 2 7 3 6
Using . pivot_table
重複するラベルの組み合わせが存在する場合、pivot_table を利用できます。デフォルトで平均値が計算されます:
out = df.pivot_table( index=['Country', 'Year'], columns='Indicator', values='Value') print(out.rename_axis(columns=None).reset_index())
結果:
Country Year 1 2 3 4 5 0 Angola 2005 6.0 13.0 10.0 11.0 5.0 1 Angola 2006 3.0 2.0 7.0 3.0 6.0
詳細については、再形成とピボット テーブルに関する Pandas ユーザー ガイドを参照してください。
以上がPandas で DataFrame を転置して行と列をピボットするにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します

Useforloopswhenthenumberofiterationsisknowninadvance、andwhiloopswheniterationsdependonacondition.1)forloopsareidealforsecenceslikelistoranges.2)


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

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

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

SublimeText3 中国語版
中国語版、とても使いやすい

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

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