การ Unstack DataFrames

การจัดรูปแบบข้อมูลด้วย pandas

Maria Eugenia Inzaugarat

Data Scientist

ทบทวน

สอง DataFrame กับ stack และ unstack method

การจัดรูปแบบข้อมูลด้วย pandas

การย้อนกลับกระบวนการ stacking

ลูกศรชี้จาก DataFrame แบบ stacked ไปยัง DataFrame แบบ unstacked

การจัดรูปแบบข้อมูลด้วย pandas

เมธอด .unstack()

กระบวนการย้อนกลับของ stack

  unstack method

การจัดรูปแบบข้อมูลด้วย pandas

เมธอด .unstack()

จัดเรียงระดับหนึ่งของ row index ให้กลายเป็นคอลัมน์ เพื่อให้ได้ DataFrame ที่ถูก reshape พร้อม column index ระดับในสุดใหม่

คอลัมน์ที่ถูกจัดเรียงใหม่และไฮไลต์ไว้

การจัดรูปแบบข้อมูลด้วย pandas

Unstack Series

churn_stacked
member  credit_card              
yes     no           credit_score        619
                     age                  43
                     country          France
                     num_products          1
                     churn               Yes
no      yes          credit_score        608
                     age                  34
                     country         Germany
                     num_products          0
                     churn                No
yes     yes          credit_score        502
                     age                  23
                     country          France
                     num_products          1
                     churn               Yes
การจัดรูปแบบข้อมูลด้วย pandas

Unstack Series

churned_stacked.unstack()
                    credit_score age  country  num_products exited
member credit_card
    no         yes           608  34  Germany             0     No
   yes          no           619  43   France             1    Yes
               yes           502  23   France             1    Yes
การจัดรูปแบบข้อมูลด้วย pandas

การ Unstack DataFrame

patients_stacked
                  year  2019 2020
  first   last feature          
   Wick   John     age    25   26 
                weight    68   72
        Julien     age    31   32
                weight    72   73
Shelley   Mary     age    41   42
                weight    68   69
         Frank     age    32   33
                weight    75   74
การจัดรูปแบบข้อมูลด้วย pandas

การ Unstack DataFrame

patients_stacked.unstack()
                       2019       2020
        feature  age weight age weight
   last   first
Shelley   Frank   32     75  33     74
           Mary   41     68  42     69
   Wick    John   25     68  26     72
         Julien   31     72  32     73
การจัดรูปแบบข้อมูลด้วย pandas

การ Unstack ตาม level

DataFrame ที่มีการ unstack ระดับที่เลือกไว้

unstack method พร้อมระบุ level

การจัดรูปแบบข้อมูลด้วย pandas

การ Unstack level ด้วยหมายเลข

churn_stacked.head(10)
member  credit_card              
yes     no           credit_score        619
                     age                  43
                     country          France
                     num_products          1
                     churn               Yes
no      yes          credit_score        608
                     age                  34
                     country         Germany
                     num_products          0
                     churn                No
churn_stacked.unstack(level=0)
                   member     no    yes
credit_card            
         no credit_score     NaN    619
                     age     NaN     43
                 country     NaN France
            num_products     NaN      1
                   churn     NaN    Yes
        yes credit_score     608    502
                     age      34     23
                 country Germany France
            num_products       0      1
                   churn      No    Yes
การจัดรูปแบบข้อมูลด้วย pandas

การ Unstack level ด้วยชื่อ

churn_stacked.head(10)
member  credit_card              
yes     no           credit_score        619
                     age                  43
                     country          France
                     num_products          1
                     churn               Yes
no      yes          credit_score        608
                     age                  34
                     country         Germany
                     num_products          0
                     churn                No
churn_stacked.unstack(level='credit_card')
             credit_card      no     yes
     member            
         no credit_score     NaN     608
                     age     NaN      34
                 country     NaN Germany
            num_products     NaN       0
                   churn     NaN      No
        yes credit_score     619     NaN
                     age      43     NaN
                 country  France     NaN
            num_products       1     NaN
                   churn     Yes     NaN
การจัดรูปแบบข้อมูลด้วย pandas

การเรียงลำดับ index

patients_stacked.unstack().sort_index(ascending=False)
           year         2019       2020
        feature   age weight age weight 
   last   first
   Wick  Julien    31     72  32     73 
           John    25     68  26     72
Shelley    Mary    41     68  42     69
          Frank    32     75  33     74
การจัดรูปแบบข้อมูลด้วย pandas

การจัดเรียง level ใหม่

patients_stacked
                  year  2019 2020
  first   last feature          
   Wick   John     age    25   26 
                weight    68   72
        Julien     age    31   32
                weight    72   73
Shelley   Mary     age    41   42
                weight    68   69
         Frank     age    32   33
                weight    75   74
patients_stacked.unstack(level=1).stack(level=0)
first                 Frank  John  Julien  Mary
 last   feature year                           
Shelley age     2019   32.0   NaN     NaN  41.0
                2020   33.0   NaN     NaN  42.0
        weight  2019   75.0   NaN     NaN  68.0
                2020   74.0   NaN     NaN  69.0
Wick    age     2019    NaN  25.0    31.0   NaN
                2020    NaN  26.0    32.0   NaN
        weight  2019    NaN  68.0    72.0   NaN
                2020    NaN  72.0    73.0   NaN
การจัดรูปแบบข้อมูลด้วย pandas

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

การจัดรูปแบบข้อมูลด้วย pandas

Preparing Video For Download...