The applymap () function of the pandas dataframe can process each value in the Dataframe and return a new dataframe:
Import= PD. DataFrame ({ 'a': [1, 2, 3], 'b': [ , c: [ 5, ten, +]} def Add_one (x): return x + 1 print df.applymap (add_one)
a b c0 2 3 4 16
A chestnut:
Here is a set of data that is 10 students of two exam results, required to convert the results to the ABCD level:
The conversion rules are:
90-100 A
80-89, B
70-79 C
60-69, D
0-59-F
GRADES_DF =PD. DataFrame (Data={'exam1': [43, 81, 78, 75, 89, 70, 91, 65, 98, 87], 'exam2': [24, 63, 56, 56, 67, 51, 79, 46, 72, 60]}, index=['Andre','Barry','Chris','Dan','Emilio', 'Fred','Greta','Humbert','Ivan','James'])
defConvert_to_letter (score):if(Score >= 90): return 'A' elif(Score >= 80): return 'B' elif(Score >= 70): return 'C' elif(Score >= 60): return 'D' Else: return 'F' defConvert_grades (Grades):returnGrades.applymap (Convert_to_letter)
Print Convert_grades (GRADES_DF)
exam1 exam2andre F fbarry b dchris c Fdan c femilio b dfred c Fgreta A chumbert d Fivan a cjames B D
Pandas DataFrame Applymap () function