0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[pandas] indexが複数あるときのタイトルの付け方 [Python]

Last updated at Posted at 2025-03-26

indexが1つだけのときと2つ以上のときとでは設定方法が異なる。

方法

indexが1つの場合

df.index.name='foo'

indexが2つ以上の場合

df.index.names=['foo','bar']

indexが1つの場合も、長さ1の配列を渡せばdf.index.namesで設定可能。

使用例

import pandas as pd

# サンプルデータの作成(MultiIndex)
arrays = [['A', 'A', 'B', 'B'], [1, 2, 1, 2]]
index = pd.MultiIndex.from_arrays(arrays, names=('カテゴリ', '番号'))
data = {'': [10, 20, 30, 40]}
df = pd.DataFrame(data, index=index)

# インデックスのタイトルを変更
df.index.names = ['新カテゴリ名', '新番号名']

print(df)

#                   値
# 新カテゴリ名 新番号名    
# A            1    10
#              2    20
# B            1    30
#              2    40

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?