2016-06-17 13 views
1

DataFrameの値の列を、ルックアップテーブルによって生成されたより正確な/完全な値のセット準備した。Seriesルックアップテーブルを使用してPandas DataFrameの列の値を置き換えます

私はこのようにすることができると思ったが、結果は期待通りではなかった。ここで

私は修正したいDATAFRAMEです:

In [6]: df_normalised.head(10) 
Out[6]: 
    code           name 
0 8        Human development 
1 11            
2 1       Economic management 
3 6   Social protection and risk management 
4 5       Trade and integration 
5 2      Public sector governance 
6 11 Environment and natural resources management 
7 6   Social protection and risk management 
8 7     Social dev/gender/inclusion 
9 7     Social dev/gender/inclusion 

は(行2で不足している名に注意してください)。ここで

は、私は固定を行うために作成されたルックアップテーブルである:ここでは

In [20]: names 
Out[20]: 
1        Economic management 
10        Rural development 
11 Environment and natural resources management 
2       Public sector governance 
3          Rule of law 
4   Financial and private sector development 
5       Trade and integration 
6   Social protection and risk management 
7      Social dev/gender/inclusion 
8        Human development 
9        Urban development 
dtype: object 

は、私はそれを行うことができると思った方法です:

In [21]: names[df_normalised.head(10).code] 
Out[21]: 
code 
8        Human development 
11 Environment and natural resources management 
1        Economic management 
6   Social protection and risk management 
5       Trade and integration 
2       Public sector governance 
11 Environment and natural resources management 
6   Social protection and risk management 
7      Social dev/gender/inclusion 
7      Social dev/gender/inclusion 
dtype: object 

しかし、私は上記の結果のシリーズを期待しましたコード値に基づくインデックスではなく、df_normalised(つまり、0,1,2,3)のインデックスと同じインデックスを持つようにします。

インデックスが同じではないため、df_normalisedの 'name'列の元の値をこれらの系列値に置き換える方法がわかりません。

ちなみに、上記のように重複する値を持つインデックスはどのようにできますか?

答えて

1

あなたはそのためmap()機能を使用することができます。優れた

In [38]: df_normalised['name'] = df_normalised['code'].map(name) 

In [39]: df_normalised 
Out[39]: 
    code           name 
0  8        Human development 
1 11 Environment and natural resources management 
2  1       Economic management 
3  6   Social protection and risk management 
4  5       Trade and integration 
5  2      Public sector governance 
6 11 Environment and natural resources management 
7  6   Social protection and risk management 
8  7     Social dev/gender/inclusion 
9  7     Social dev/gender/inclusion 
+0

を。ありがとう!私は地図を見ましたが、機能を適用するだけだと思っていました。 – Bill

0

これは機能します。しかし、私はこれを行う簡単な方法が必要であると確信しています。

In [50]: df_normalised.name = pd.Series(names[df_normalised.code].values) 

In [51]: df_normalised.head(10) 
Out[51]: 
    code           name 
0 8        Human development 
1 11 Environment and natural resources management 
2 1       Economic management 
3 6   Social protection and risk management 
4 5       Trade and integration 
5 2      Public sector governance 
6 11 Environment and natural resources management 
7 6   Social protection and risk management 
8 7     Social dev/gender/inclusion 
9 7     Social dev/gender/inclusion 
関連する問題