Quantcast
Channel: dictionaryタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 99

あるシリーズの文字列が辞書のKeyにあった時、辞書のValueに文字列を変換させる

$
0
0

やりたいこと : あるSeriesが辞書のkeyにあった時、辞書のvalueに値を変換させる

【元のデータ】
hoge = pd.Series(["3回目", "1回目", "5回目", "5回目", "2回目", "4回目"])

【変換リスト】
name_lis = {"1回目": "01.first",
"2回目": "02.second",
"3回目": "03.third",
"4回目": "04.fourth",
"5回目": "05.fifth"
}

mapでSeriesを囲う(@nkayさんありがとうございます!)

hoge.map(name_lis)

辞書でSeriesを囲う

hoge.apply(lambda x: name_lis[x])

0 03.third
1 01.first
2 05.fifth
3 05.fifth
4 02.second
5 04.fourth
dtype: object


Viewing all articles
Browse latest Browse all 99

Trending Articles