2017-04-22 4 views
5

मेरे पास डेटाफ्रेम और नीचे एक निर्देश है, लेकिन मैं कॉलम को निर्देश से कैसे बदलूं?एक संख्या-कीवर्ड dict द्वारा एक शुद्ध-संख्या कॉलम को कैसे प्रतिस्थापित करें? [पायथन]

data 
index  occupation_code 
0   10 
1   16 
2   12 
3   7 
4   1 
5   3 
6   10 
7   7 
8   1 
9   3 
10   4 
…… 

dict1 = {0: 'other',1: 'academic/educator',2: 'artist',3: 'clerical/admin',4: 'college/grad student',5: 'customer service',6: 'doctor/health care',7: 'executive/managerial',8: 'farmer',9: 'homemaker',10: 'K-12student',11: 'lawyer',12: 'programmer',13: 'retired',14: 'sales/marketing',15: 'scientist',16: 'self-employed',17: 'technician/engineer',18: 'tradesman/craftsman',19: 'unemployed',20: 'writer'} 

मैं की जगह करने के लिए एक वाक्य "के लिए" का इस्तेमाल किया, लेकिन यह बहुत धीमी है, उस तरह:

for i in data.index: 
    data.loc[i,'occupation_detailed'] = dict1[data.loc[i,'occupation_code']] 

के बाद से अपने डेटा 1 लाख लाइनों होता है और यह कई सेकंड लागत अगर मैं केवल रन यह 1 हजार बार के लिए। 1 मिलियन लाइन का आधा दिन खर्च हो सकता है!

तो क्या ऐसा करने का कोई बेहतर तरीका है?

आपके सुझावों के लिए बहुत बढ़िया धन्यवाद!

उत्तर

7

उपयोग map और NaN कुछ मान अनुपलब्ध मिलता है:

print (df) 
     occupation_code 
index     
0     10 
1     16 
2     12 
3     7 
4     1 
5     3 
6     10 
7     7 
8     1 
9     3 
10     4 
11     100 <- add missing value 100 

df['occupation_code'] = df['occupation_code'].map(dict1) 
print (df) 
      occupation_code 
index      
0    K-12student 
1    self-employed 
2    programmer 
3  executive/managerial 
4   academic/educator 
5   clerical/admin 
6    K-12student 
7  executive/managerial 
8   academic/educator 
9   clerical/admin 
10  college/grad student 
11      NaN 

एक अन्य समाधान replace का उपयोग कर रहा है, अगर कुछ मान मूल मूल्य प्राप्त लापता, कोई NaN:

df['occupation_code'] = df['occupation_code'].replace(dict1) 
print (df) 
      occupation_code 
index      
0    K-12student 
1    self-employed 
2    programmer 
3  executive/managerial 
4   academic/educator 
5   clerical/admin 
6    K-12student 
7  executive/managerial 
8   academic/educator 
9   clerical/admin 
10  college/grad student 
11      100 
+0

यह बहुत तेज़ चलता है! बहुत धन्यवाद! – Ricky

1

@ जेज़राइल के नमूना डेटामानते हैं

print(df) 

     occupation_code 
index     
0     10 
1     16 
2     12 
3     7 
4     1 
5     3 
6     10 
7     7 
8     1 
9     3 
10     4 
11     100 

मैं एक शब्दकोश एक lambda में एम्बेडेड के get विधि का उपयोग की सलाह देते हैं। यह आपको उन चीज़ों के लिए डिफ़ॉल्ट मान एम्बेड करने की अनुमति देता है जो शब्दकोश में नहीं हैं। इस मामले में, मैं मूल मूल्य वापस कर देता हूं।

df.occupation_code.map(lambda x: dict1.get(x, x)) 

index 
0    K-12student 
1   self-employed 
2    programmer 
3  executive/managerial 
4  academic/educator 
5   clerical/admin 
6    K-12student 
7  executive/managerial 
8  academic/educator 
9   clerical/admin 
10 college/grad student 
11      100 
Name: occupation_code, dtype: object 
संबंधित मुद्दे