Pandas Exercise (iv)---apply apply function

Source: Internet
Author: User

Explore the students ' consumption of wine

Data See GitHub

Step 1-Import the necessary libraries
Import Pandas as PD Import NumPy as NP
Step 2-Data set
" ./data/student-mat.csv "  
Step 3 Name The data student
Student = Pd.read_csv (PATH4) Student.head ()

Output:

Step 4 Slice the data from ' school ' to ' Guardian '
" School ":"Guardian"]stud_alcoh.head ()

Output:

Step 5 Create a lambda function that captures a string
Lambda x:x.upper ()
Step 6 capitalize the ' Fjob ' column
stud_alcoh['Fjob'].apply (Captalizer)

Output:

 0 TEACHER1 OTHER2 OTHER3 SERVICES4 OTHER5 OT HER6 OTHER7 TEACHER8 OTHER9 OTHER10 HEALTH11 OTHER12 SERVICES13 othe R14 OTHER15 OTHER16 SERVICES17 OTHER18 SERVICES19 OTHER20 OTHER21 HEALTH2         2 OTHER23 OTHER24 HEALTH25 SERVICES26 OTHER27 SERVICES28 OTHER29 TEACHER   ... 365 OTHER366 SERVICES367 SERVICES368 SERVICES369 TEACHER370 SERVICES371 SERVICES372 at_home37       3 OTHER374 OTHER375 OTHER376 OTHER377 SERVICES378 OTHER379 OTHER380 TEACHER381       OTHER382 SERVICES383 SERVICES384 OTHER385 OTHER386 at_home387 OTHER388 SERVICES389  OTHER390 SERVICES391 SERVICES392 OTHER393 OTHER394 at_homename:fjob, Dtype:object  
Step 7 Print the last few line elements of the dataset
Stud_alcoh.tail ()

Output:

Step 8 Notice that the original data frame is still lowercase and then improve
stud_alcoh['mjob'= stud_alcoh['mjob'].apply ( Captalizer) stud_alcoh['Fjob'] = stud_alcoh['Fjob ' ].apply (Captalizer) stud_alcoh.tail ()

Output:

Step 9 Create a function named majority, which returns a Boolean value to a new column named Legal_drinker (most older than 17 years old)
def majority (x):     if x >:        return     TrueElse:          return False
stud_alcoh[' legal_drinker'= stud_alcoh['age'].apply ( Majority) Stud_alcoh.head ()

Output:

Step 10 multiply each number of datasets by 10
def TIMES10 (x):     if  is int:         return * x    return x
Stud_alcoh.applymap (TIMES10). Head (10)

Output:

Pandas Exercise (iv)---apply apply 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: info-contact@alibabacloud.com 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.