數據見githubhtml
import pandas as pd import numpy as np
path4 = "./data/student-mat.csv"
student = pd.read_csv(path4)
student.head()
輸出:python
stud_alcoh = student.loc[: , "school":"guardian"] stud_alcoh.head()
輸出:git
captalizer = lambda x: x.upper()
stud_alcoh['Fjob'].apply(captalizer)
輸出:github
0 TEACHER
1 OTHER
2 OTHER
3 SERVICES
4 OTHER
5 OTHER
6 OTHER
7 TEACHER
8 OTHER
9 OTHER
10 HEALTH
11 OTHER
12 SERVICES
13 OTHER
14 OTHER
15 OTHER
16 SERVICES
17 OTHER
18 SERVICES
19 OTHER
20 OTHER
21 HEALTH
22 OTHER
23 OTHER
24 HEALTH
25 SERVICES
26 OTHER
27 SERVICES
28 OTHER
29 TEACHER
...
365 OTHER
366 SERVICES
367 SERVICES
368 SERVICES
369 TEACHER
370 SERVICES
371 SERVICES
372 AT_HOME
373 OTHER
374 OTHER
375 OTHER
376 OTHER
377 SERVICES
378 OTHER
379 OTHER
380 TEACHER
381 OTHER
382 SERVICES
383 SERVICES
384 OTHER
385 OTHER
386 AT_HOME
387 OTHER
388 SERVICES
389 OTHER
390 SERVICES
391 SERVICES
392 OTHER
393 OTHER
394 AT_HOME
Name: Fjob, dtype: object
stud_alcoh.tail()
輸出:app
stud_alcoh['Mjob'] = stud_alcoh['Mjob'].apply(captalizer) stud_alcoh['Fjob'] = stud_alcoh['Fjob'].apply(captalizer) stud_alcoh.tail()
輸出:函數
def majority(x): if x > 17: return True else: return False
stud_alcoh['legal_drinker'] = stud_alcoh['age'].apply(majority) stud_alcoh.head()
輸出:ui
def times10(x): if type(x) is int: return 10 * x return x
stud_alcoh.applymap(times10).head(10)
輸出:spa
一、http://pandas.pydata.org/pandas-docs/stable/cookbook.html#cookbook3d
二、https://www.analyticsvidhya.com/blog/2016/01/12-pandas-techniques-python-data-manipulation/code