सूचियों की मदद से मान बदलें

pandas के साथ कुशल कोड लिखना

Leonidas Souliotis

PhD Candidate

कई मानों को एक मान से बदलें

जन्म वर्ष लिंग जातीयता बच्चे का पहला नाम संख्या रैंक
2011 FEMALE WHITE NON HISP HELENA 97 4
start_time = time.time()
names['Ethnicity'].loc[(names["Ethnicity"] == 'WHITE NON HISPANIC') | 
(names["Ethnicity"] == 'WHITE NON HISP')] = 'WNH'
print("Results from the above operation calculated in %s seconds" %
 (time.time() - start_time))
Results from the second method calculated in 0.0276169776917 seconds
pandas के साथ कुशल कोड लिखना

.replace() से कई मान बदलें I

start_time = time.time()
names['Ethnicity'].replace(['WHITE NON HISPANIC','WHITE NON HISP'],
'WNH', inplace=True)
print("Time using .replace(): {} sec".format(time.time() - start_time))
Time using .replace():  0.00144791603088 sec
Difference in speed: 2160.68681809%
pandas के साथ कुशल कोड लिखना
names['Ethnicity'].replace(['WHITE NON HISP'], 'WHITE NON HISPANIC', inplace=True)
names['Ethnicity'].replace(['BLACK NON HISP'], 'BLACK NON HISPANIC', inplace=True)
names['Ethnicity'].replace(['BLACK NON HISP','WHITE NON HISP'], ['BLACK NON HISPANIC',
'WHITE NON HISPANIC'], inplace=True)
pandas के साथ कुशल कोड लिखना

आइए इसे करें

pandas के साथ कुशल कोड लिखना

Preparing Video For Download...