Pandas DataFrame インデックスを削除

Suraj Joshi 2023年1月30日 2021年1月23日
  1. インデックスを削除するには reset_index() メソッドを使用する
  2. set_index() メソッドを用いた Pandas DataFrame のインデックスの削除
Pandas DataFrame インデックスを削除

このチュートリアルでは、Pandas DataFrame のインデックスを削除する方法を説明します。

下に表示されている DataFrame を使用して、インデックスを削除する方法を説明します。

import pandas as pd

my_df = pd.DataFrame({
    'Person': ['Alice', 'Steven', 'Neesham', 'Chris', 'Alice'],
    'City': ['Berlin', 'Montreal', 'Toronto', 'Rome', 'Munich'],
    'Mother Tongue': ['German', 'French', 'English', 'Italian', 'German'],
    'Age':  [37, 20, 38, 23, 35],

},index=["A","B","C","D","E"])

print(my_df)

出力:

    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

インデックスを削除するには reset_index() メソッドを使用する

pandas.DataFrame.reset_index() は DataFrame のインデックスをデフォルトのインデックスにリセットします。

import pandas as pd

my_df = pd.DataFrame({
    'Person': ['Alice', 'Steven', 'Neesham', 'Chris', 'Alice'],
    'City': ['Berlin', 'Montreal', 'Toronto', 'Rome', 'Munich'],
    'Mother Tongue': ['German', 'French', 'English', 'Italian', 'German'],
    'Age':  [37, 20, 38, 23, 35],

},index=["A","B","C","D","E"])

df_reset=my_df.reset_index()

print("Before reseting Index:")
print(my_df,"\n")

print("After reseting Index:")
print(df_reset)

出力:

Before reseting Index:
    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

After reseting Index:
  index   Person      City Mother Tongue  Age
0     A    Alice    Berlin         German   37
1     B   Steven  Montreal         French   20
2     C  Neesham   Toronto        English   38
3     D    Chris      Rome        Italian   23
4     E    Alice    Munich         German   35

この関数は my_df DataFrame のインデックスをリセットしますが、インデックスは index カラムとして表示されるようになります。index カラムを削除したい場合は、reset_index() メソッドで drop=True を指定すればよい。

import pandas as pd

my_df = pd.DataFrame({
    'Person': ['Alice', 'Steven', 'Neesham', 'Chris', 'Alice'],
    'City': ['Berlin', 'Montreal', 'Toronto', 'Rome', 'Munich'],
    'Mother Tongue': ['German', 'French', 'English', 'Italian', 'German'],
    'Age':  [37, 20, 38, 23, 35],

},index=["A","B","C","D","E"])

df_reset=my_df.reset_index(drop=True)

print("Before reseting Index:")
print(my_df,"\n")

print("After reseting Index:")
print(df_reset)

出力:

Before reseting Index:
    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

After reseting Index:
    Person      City Mother Tongue  Age
0    Alice    Berlin         German   37
1   Steven  Montreal         French   20
2  Neesham   Toronto        English   38
3    Chris      Rome        Italian   23
4    Alice    Munich         German   35

set_index() メソッドを用いた Pandas DataFrame のインデックスの削除

pandas.DataFrame.set_index() は、引数に渡されたカラムを初期インデックスを上書きして DataFrame のインデックスとして設定します。

import pandas as pd

my_df = pd.DataFrame({
    'Person': ['Alice', 'Steven', 'Neesham', 'Chris', 'Alice'],
    'City': ['Berlin', 'Montreal', 'Toronto', 'Rome', 'Munich'],
    'Mother Tongue': ['German', 'French', 'English', 'Italian', 'German'],
    'Age':  [37, 20, 38, 23, 35],

},index=["A","B","C","D","E"])

df_reset=my_df.set_index('Person')

print("Initial DataFrame:")
print(my_df,"\n")

print("After setting Person column as Index:")
print(df_reset)

出力:

Initial DataFrame:
    Person      City Mother Tongue  Age
A    Alice    Berlin         German   37
B   Steven  Montreal         French   20
C  Neesham   Toronto        English   38
D    Chris      Rome        Italian   23
E    Alice    Munich         German   35

After setting Person column as Index:
             City Mother Tongue  Age
Person
Alice      Berlin         German   37
Steven   Montreal         French   20
Neesham   Toronto        English   38
Chris        Rome        Italian   23
Alice      Munich         German   35

Person カラムを my_df DataFrame のインデックスとして設定します。

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

関連記事 - Pandas Index