2016-03-31 7 views
0

私はpythonを初めて使い、このために多数のページを見てきました。辞書からの地図リスト

私はパンダのデータフレームを知っているが、このマッピングfunctionlityを持っている:

dictionary = {a:1, b:2, c:6} 

df['col_name'] = df.col_name.map(dictionary) #df is a pandas dictionary 

は私がどこ

list_to_be_mapped = [a,a,b,c,c,a] 
mapped_list  = [1,1,2,6,6,1] 
+0

'mapped_list'はあなたの所在地に従って' [1,1,2,6,6,1] 'にするべきですか? –

+0

はい、私は変更を実装しました。それは小さなタイプミスでした。しかし、これは答えを変更しません!ありがとうございました。 –

答えて

3

にすることができますリストの似たような、すなわち、

mapped_list = list_to_be_mapped.map(dictionary) 

をどのように行うのですかdictionaryget機能

を使用してください
list(map(dictionary.get, list_to_be_mapped)) 
2

IIUCあなたはそのための簡単なlist comprehensionを使用することができます。

[dictionary[key] for key in list_to_be_mapped] 

In [51]: [dictionary[key] for key in list_to_be_mapped] 
Out[51]: [1, 1, 2, 6, 6, 1] 

あなたがpandasソリューションを好む場合は、シリーズにごlist_to_be_mappedを変換し、あなたの例のように同じを使用することができます。

s = pd.Series(list_to_be_mapped) 

In [53]: s 
Out[53]: 
0 a 
1 a 
2 b 
3 c 
4 c 
5 a 
dtype: object 

In [55]: s.map(dictionary).tolist() 
Out[55]: [1, 1, 2, 6, 6, 1]