Pandas DataFrame Applymap () function

Source: Internet
Author: User

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

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.