LoginSignup
21
24

More than 5 years have passed since last update.

Python データフレームの列名を変更

Posted at

pandasでのデータ加工時、データフレームの列名を変更したいとき

1.データ読み込み時

df = pd.read_csv('test.csv',
                  names = ('id', 'gender', 'generation'))

変更後イメージ

id gender generation
aa 0 10
bb 1 20

2.データ読み込み後

現在ついている列名を何に変更するかを、columnsの後に辞書型で指定する

#変更前のデータフレーム名をdfとする
df = df.rename(columns={'A':'id', 'B':'gender', 'C':'generation'})

変更前イメージ

A B C
aa 0 10
bb 1 20

変更後イメージ

id gender generation
aa 0 10
bb 1 20
21
24
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
21
24