2017-05-29 9 views
2

私はまだPythonを学んでおり、かなり大きな行列に属するベクトルがあり、このベクトルのエントリはオブジェクト型です。それらは( '< 1年'、 '1年'、 '2年'など) それぞれ0,1,2,3に変更したいと思います。私は仕事に以下の行を書きましたが、10回の条件でループを必要としない、より簡単な解決策が存在しなければならない:Pythonの行列で複数の文字を置換する

import numpy as np 
import pandas as pd 

data_file = pd.read_csv('loan.csv') 

emp_length=data_file.emp_length 
emp_len=[] 
for i in range(len(emp_length)): 
    if emp_length[i]=='< 1 year': 
     emp_len.append(0) 
    elif emp_length[i]=='1 year': 
     emp_len.append(1) 
    elif emp_length[i]=='2 years': 
     emp_len.append(2) 
    elif emp_length[i]=='3 years': 
     emp_len.append(3) 
    elif emp_length[i]=='4 years': 
     emp_len.append(4) 
    elif emp_length[i]=='5 years': 
     emp_len.append(5) 
    elif emp_length[i]=='6 years': 
     emp_len.append(6) 
    elif emp_length[i]=='7 years': 
     emp_len.append(7) 
    elif emp_length[i]=='8 years': 
     emp_len.append(8) 
    elif emp_length[i]=='9 years': 
     emp_len.append(9) 
    elif emp_length[i]=='10+ years': 
     emp_len.append(10) 
    else: 
     emp_len.append(0) 

私は新しいベクトルを作成する必要はありませんが、これは私がいたソリューションでした自分で思いつくことができる。同じベクター内のそれらのエントリーを置き換えることができれば、さらに良いでしょう。任意の提案をありがとうと

答えて

2

df

np.random.seed([3,1415]) 
df = pd.DataFrame(dict(emp_length=np.random.choice(list(m.keys()), 20))) 
print(df) 

    emp_length 
0 < 1 year 
1  2 years 
2 10+ years 
3 10+ years 
4  7 years 
5 10+ years 
6  3 years 
7  8 years 
8  7 years 
9 10+ years 
10 < 1 year 
11 6 years 
12 8 years 
13 6 years 
14 < 1 year 
15 10+ years 
16 2 years 
17 < 1 year 
18 4 years 
19 9 years 

あなたは

m = { 
    '< 1 year': 0, 
    '1 year': 1, 
    '2 years': 2, 
    '3 years': 3, 
    '4 years': 4, 
    '5 years': 5, 
    '6 years': 6, 
    '7 years': 7, 
    '8 years': 8, 
    '9 years': 9, 
    '10+ years': 10 
} 

data_file.emp_length.map(m) 
# or equivalently 
# data_file.emp_length.replace(m) 

0  0 
1  2 
2  10 
3  10 
4  7 
5  10 
6  3 
7  8 
8  7 
9  10 
10  0 
11  6 
12  8 
13  6 
14  0 
15 10 
16  2 
17  0 
18  4 
19  9 
Name: emp_length, dtype: int64 

ます。また、カテゴリタイプを使用することができ、辞書でmapまたはreplaceを使用することができますデータフレームを考える手助け

cats = ['< 1 year', '1 year', '2 years', '3 years', '4 years', '5 years', '6 years', '7 years', '8 years', '9 years', '10+ years'] 
c = df.emp_length.astype('category', categories=cats, ordered=True) 
print(c) 

0  < 1 year 
1  2 years 
2  10+ years 
3  10+ years 
4  7 years 
5  10+ years 
6  3 years 
7  8 years 
8  7 years 
9  10+ years 
10  < 1 year 
11  6 years 
12  8 years 
13  6 years 
14  < 1 year 
15 10+ years 
16  2 years 
17  < 1 year 
18  4 years 
19  9 years 
Name: emp_length, dtype: category 
Categories (11, object): [< 1 year < 1 year < 2 years < 3 years ... 7 years < 8 years < 9 years < 10+ years] 

次に、あなたは

c.cat.codes 

0  0 
1  2 
2  10 
3  10 
4  7 
5  10 
6  3 
7  8 
8  7 
9  10 
10  0 
11  6 
12  8 
13  6 
14  0 
15 10 
16  2 
17  0 
18  4 
19  9 
dtype: int8 
+0

偉大でマッピングされた整数をアクセスすることができました!私はpd.factorizeはうまくいかないと思いますが、「10 +年」の場合は1を返しますが、マップは非常に役に立ちました。 –

+1

回答が役に立ちましたら、[受け入れる](http: /meta.stackexchange.com/a/5235/295067)それ。ありがとう。 – jezrael

+1

それは素晴らしい答えです! – MaxU

関連する問題