Pandas เพิ่มเติม

Python สำหรับผู้ใช้ R

Daniel Chen

Instructor

ข้อมูลที่หายไป

  • ค่า NaN ที่หายไปมาจาก numpy
  • np.NaN, np.NAN, np.nan เทียบเท่ากับค่า NA ใน R
  • ตรวจสอบค่าที่หายไปด้วย pd.isnull
    • ตรวจสอบค่าที่ไม่หายไปด้วย pd.notnull
    • pd.isnull เป็น alias ของ pd.isna
Python สำหรับผู้ใช้ R

การทำงานกับข้อมูลที่หายไป

df
           name  treatment_a  treatment_b
0    John Smith          NaN            2
1      Jane Doe         16.0           11
2  Mary Johnson          3.0            1
a_mean = df['treatment_a'].mean()
a_mean
9.5
Python สำหรับผู้ใช้ R

Fillna

df['a_fill'] = df['treatment_a'].fillna(a_mean)
df
           name  treatment_a  treatment_b  a_fill
0    John Smith          NaN            2     9.5
1      Jane Doe         16.0           11    16.0
2  Mary Johnson          3.0            1     3.0
Python สำหรับผู้ใช้ R

Pandas เพิ่มเติม

  • การใช้ฟังก์ชันที่กำหนดเอง
  • การดำเนินการ Groupby
  • การจัดระเบียบข้อมูล
Python สำหรับผู้ใช้ R

การใช้ฟังก์ชันของตัวเอง

  • ฟังก์ชันที่มีในตัว
  • ฟังก์ชันที่กำหนดเอง
  • เมธอด apply
  • ระบุแกนด้วย axis
Python สำหรับผู้ใช้ R
R
df = data.frame('a' = c(1, 2, 3),
                'b' = c(4, 5, 6))
apply(df, 2, mean)
a b 
2 5 
apply(df, 1, mean)
2.5 3.5 4.5
Python
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3],
                   'B':[4, 5, 6]})
df.apply(np.mean, axis=0)
A    2.0
B    5.0
dtype: float64
df.apply(np.mean, axis=1)
0    2.5
1    3.5
2    4.5
dtype: float64
Python สำหรับผู้ใช้ R

Tidy

  • การจัดรูปแบบและระเบียบข้อมูล
  • Hadley Wickham, Tidy Data Paper
    • แต่ละแถวคือการสังเกตหนึ่งครั้ง
    • แต่ละคอลัมน์คือตัวแปรหนึ่งตัว
    • แต่ละประเภทของหน่วยสังเกตรวมเป็นตาราง

Tidy Data Paper: http://vita.had.co.nz/papers/tidy-data.pdf

Python สำหรับผู้ใช้ R

Tidy melt

df
           name  treatment_a  treatment_b
0    John Smith          NaN            2
1      Jane Doe         16.0           11
2  Mary Johnson          3.0            1
df_melt = pd.melt(df, id_vars='name')
df_melt
           name     variable  value
0    John Smith  treatment_a    NaN
1      Jane Doe  treatment_a   16.0
2  Mary Johnson  treatment_a    3.0
3    John Smith  treatment_b    2.0
...
Python สำหรับผู้ใช้ R

Tidy pivot_table

df_melt_pivot = pd.pivot_table(df_melt,
                               index='name',
                               columns='variable',
                               values='value')
df_melt_pivot
variable      treatment_a  treatment_b
name                                  
Jane Doe             16.0         11.0
John Smith            NaN          2.0
Mary Johnson          3.0          1.0
Python สำหรับผู้ใช้ R

Reset index

df_melt_pivot.reset_index()
variable          name  treatment_a  treatment_b
0             Jane Doe         16.0         11.0
1           John Smith          NaN          2.0
2         Mary Johnson          3.0          1.0
Python สำหรับผู้ใช้ R

Groupby

  • groupby: แบ่ง-ประมวลผล-รวม
  • แบ่งข้อมูลออกเป็นส่วนย่อย
  • ใช้ฟังก์ชันกับแต่ละส่วน
  • รวมผลลัพธ์
Python สำหรับผู้ใช้ R

การใช้งาน groupby

           name     variable  value
0    John Smith  treatment_a    NaN
1      Jane Doe  treatment_a   16.0
2  Mary Johnson  treatment_a    3.0
3    John Smith  treatment_b    2.0
4      Jane Doe  treatment_b   11.0
5  Mary Johnson  treatment_b    1.0
df_melt.groupby('name')['value'].mean()
name
Jane Doe        13.5
John Smith       2.0
Mary Johnson     2.0
Name: value, dtype: float64
Python สำหรับผู้ใช้ R

มาฝึกกันเถอะ!

Python สำหรับผู้ใช้ R

Preparing Video For Download...