2016-06-17 5 views
1

DataFrame의 값 열을 룩업 테이블에서 생성 된 값의보다 정확하고 완전한 값 세트로 바꾸고 싶습니다. 준비했다.Series 조회 테이블을 사용하여 Pandas 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에서 누락 된 이름을 참고) : 여기

내가 해결하려는 DataFrame입니다.

다음
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 
관련 문제