このPythonチュートリアルでは、DataFrameのインデックスをカラムに変換する方法について説明します。
また、複数インデックスのDataFrameの複数レベルのインデックスをその複数カラムに変換する方法についても見ていきます。
それでは、はじめましょう。
pandas DataFrameのインデックスとは?
Pandasはデータ解析に広く利用されている堅牢なPythonライブラリです。
PandasはDataFrameと呼ばれるデータ構造を提供し、データを行と列の形式で保存します。
pandasのDataFrameオブジェクトは複数のインデックスのレベルを持つことができ、その場合MultiIndex DataFrameと呼ばれます。
panda DataFrameオブジェクトを作成すると、デフォルトでは0から行数-1までのインデックス値がDataFrameの各行に対して順番に割り当てられます。
しかし、pandasの DataFrame.set_index() 関数を用いて、pandas DataFrameオブジェクトの各行にインデックス値を手動で設定することも可能です。
以下の2つのメソッドを使って、pandas DataFrameオブジェクトの1レベル以上のインデックスをそのカラムに変換することができます。
DataFrameのインデックスをカラムに変換する処理を示すために、まずpandas DataFrameオブジェクトを作成してみましょう。
こちらもお読みください。
PandasのDataFrameのインデックス作成。
この記事もチェック:Pandasのreplaceメソッドを使って複数の値を置換する方法
Pandasデータフレームのインデックスをカラムに変換するメソッド
# Import pandas Python moduleimport pandas as pd
# Create a pandas DataFrame objectdf = pd.DataFrame({'Dept': ['ECE', 'ICE', 'IT', 'CSE', 'CHE'],
'GPA': [8.15, 9.03, 7.85, 8.55, 9.45],
'Name': ['Kirti', 'Sarthak', 'Anubhav', 'Ranjan', 'Kartik'],
'RegNo': [111, 112, 113, 114, 115]})
# Set 'RegNo' as index of the pandas DataFramedf.set_index('RegNo', inplace=True)
# Print the created pandas DataFrame objectprint('Sample pandas DataFrame:)
print(df)
|
結果は以下の通りです。
Sample pandas DataFrame: Dept GPA Name
RegNo 111 ECE 8.15 Kirti112 ICE 9.03 Sarthak113 IT 7.85 Anubhav114 CSE 8.55 Ranjan115 CHE 9.45 Kartik |
この記事もチェック:Pandasのデータフレームに行を追加する5つの簡単な方法
方法1:新しいDataFrameのカラムを作成し、インデックスを渡します。
これは、DataFrameのインデックスをカラムに変換する最もシンプルな方法です。
この方法では、DataFrameに新しい列を作成し、pandas DataFrameクラスの DataFrame.indexメソッドを使用してインデックスを渡します。
このメソッドを実装するためのPythonコードを見てみましょう。
# Method 1# Convert the index of the sample DataFrame into column# Using the new column methoddf['Roll'] = df.index
# Print the modified pandas DataFrameprint('Modified pandas DataFrame:)
print(df)
|
結果は以下の通りです。
Modified pandas DataFrame: Dept GPA Name Roll
RegNo 111 ECE 8.15 Kirti 111112 ICE 9.03 Sarthak 112113 IT 7.85 Anubhav 113114 CSE 8.55 Ranjan 114115 CHE 9.45 Kartik 115 |
方法2:pandasのDataFrame.reset_index()関数を使用します。
これは、DataFrameのインデックスの1つ以上のレベルを1つ以上のカラムにするために広く使用されている方法です。
この方法では、pandasのDataFrameクラスの DataFrame.reset_index() 関数を使用します。
このメソッドを実装するためのPythonコードを記述してみましょう。
# Method 2# Convert the index of the sample DataFrame into column# Using the DataFrame.reset_index() functiondf.reset_index(inplace=True)
# Print the modified pandas DataFrameprint('Modified pandas DataFrame:)
print(df)
|
結果は以下の通りです。
Modified pandas DataFrame: RegNo Dept GPA Name
0 111 ECE 8.15 Kirti1 112 ICE 9.03 Sarthak2 113 IT 7.85 Anubhav3 114 CSE 8.55 Ranjan4 115 CHE 9.45 Kartik |
MultiIndex DataFrameの1つまたは複数のレベルをカラムに変換する
まず、サンプルDataFrameの複数レベルのインデックスとして RegNo と Name を DataFrame.set_index() 関数で設定し、上記のサンプルDataFrameをMultiIndex DataFrameに変換してみましょう。
# Convert the sample DataFrame into MultiIndex DataFrame# By setting the 'RegNo' and 'Name' as Multi-level indexdf.set_index(['RegNo', 'Name'], inplace=True)
# Print the modified pandas DataFrameprint('Modified Sample pandas DataFrame:)
print(df)
|
結果は以下の通りです。
出力:
Modified Sample pandas DataFrame: Dept GPA
RegNo Name 111 Kirti ECE 8.15112 Sarthak ICE 9.03113 Anubhav IT 7.85114 Ranjan CSE 8.55115 Kartik CHE 9.45 |
次に、DataFrame.reset_index()関数を使用して、サンプルのMultiIndex DataFrameのインデックスレベルの1つだけを列に変換するPythonコードを記述してみましょう。
# Convert one level of the MultiIndex DataFrame into column# Using the DataFrame.reset_index() functiondf.reset_index(level='Name', inplace=True)
# Print the modified pandas DataFrameprint('Modified pandas DataFrame:)
print(df)
|
結果を出力すると、以下の様になります。
Modified pandas DataFrame: Name Dept GPA
RegNo 111 Kirti ECE 8.15112 Sarthak ICE 9.03113 Anubhav IT 7.85114 Ranjan CSE 8.55115 Kartik CHE 9.45 |
まとめ
この記事では、pandas DataFrameのインデックスをカラムに変換する方法を学びました。
また、MultiIndex DataFrameのインデックスの1つ以上のレベルをカラムに変換する方法を学びました。
以上のことを理解し、自分のpandas DataFrameで実験する準備ができていることを願っています。
お読みいただきありがとうございます。
Pythonプログラミングに関連する素晴らしい学習コンテンツにご期待ください。