pandas DataFrame applymap()函数

【pandas DataFrame applymap()函数】古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述pandas DataFrame applymap()函数相关的知识,希望能为你提供帮助。
pandas DataFrame的  applymap()  函数可以对DataFrame里的每个值进行处理,然后返回一个新的DataFrame:

import pandas as pddf = pd.DataFrame({ ‘a‘: [1, 2, 3], ‘b‘: [10, 20, 30], ‘c‘: [5, 10, 15] })def add_one(x): return x + 1print df.applymap(add_one)

abc 02116 132111 243116

一个栗子:
这里有一组数据是10个学生的两次考试成绩,要求把成绩转换成ABCD等级:
转换规则是:
90-100 -> A
80-89-> B
70-79-> C
60-69-> D
0-59-> F
grades_df = pd.DataFrame( data=https://www.songbingjia.com/android/{‘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‘] )

def convert_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‘def convert_grades(grades): return grades.applymap(convert_to_letter)

print convert_grades(grades_df)

exam1 exam2 AndreFF BarryBD ChrisCF DanCF EmilioBD FredCF GretaAC HumbertDF IvanAC JamesBD

 







    推荐阅读