LoginSignup
0
2

More than 1 year has passed since last update.

python mapを使った置換 pandas

Posted at

dataframeの値を置換する
sex_dictを作って、置換する組み合わせを作成する。
dataframeの中の性別をmapを使用して文字に置換する。

import pandas as pd

sex_dict = {1:'',2:'',3:'出生前',4:'不明'}

df = pd.DataFrame(
    data ={'年齢': [10, 20, 30, 40], 
          '性別': [1, 2, 1, 3],
          '列3': ['a', 'b', 'c', 'd']}
    )

print(df)
#=>
  年齢  性別 列3
0  10   1  a
1  20   2  b
2  30   1  c
3  40   3  d

#map使用
df['性別'] = df['性別'].map(sex_dict)

print(df)
#=>
  年齢   性別 列3
0  10      a
1  20      b
2  30      c
3  40  出生前  d

参考:

0
2
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
2