การปรับรูปแบบข้อมูลด้วย .melt()

การรวมข้อมูลด้วย pandas

Aaren Stubberfield

Instructor

ข้อมูลแบบ Wide กับแบบ Long

รูปแบบ Wide ข้อมูลในรูปแบบ wide

รูปแบบ Long ข้อมูลในรูปแบบ long

การรวมข้อมูลด้วย pandas

เมธอด .melt() ทำอะไร?

  • เมธอด melt ช่วย unpivot ชุดข้อมูลได้ ภาพแสดงการ Unpivot
การรวมข้อมูลด้วย pandas

ชุดข้อมูลในรูปแบบ Wide

ตารางนี้ชื่อ social_fin

  financial      company   2019      2018      2017      2016    
0 total_revenue  twitter   3459329   3042359   2443299   2529619 
1 gross_profit   twitter   2322288   2077362   1582057   1597379 
2 net_income     twitter   1465659   1205596   -108063   -456873 
3 total_revenue  facebook  70697000  55838000  40653000  27638000
4 gross_profit   facebook  57927000  46483000  35199000  23849000
5 net_income     facebook  18485000  22112000  15934000  10217000
การรวมข้อมูลด้วย pandas

ตัวอย่างการใช้ .melt()

social_fin_tall = social_fin.melt(id_vars=['financial','company'])
print(social_fin_tall.head(10))
  financial      company   variable  value   
0 total_revenue  twitter   2019      3459329 
1 gross_profit   twitter   2019      2322288 
2 net_income     twitter   2019      1465659 
3 total_revenue  facebook  2019      70697000
4 gross_profit   facebook  2019      57927000
5 net_income     facebook  2019      18485000
6 total_revenue  twitter   2018      3042359 
7 gross_profit   twitter   2018      2077362 
8 net_income     twitter   2018      1205596 
9 total_revenue  facebook  2018      55838000
การรวมข้อมูลด้วย pandas

การใช้ melt ร่วมกับ value_vars

social_fin_tall = social_fin.melt(id_vars=['financial','company'], 
                                  value_vars=['2018','2017'])
print(social_fin_tall.head(9))
  financial      company   variable  value   
0 total_revenue  twitter   2018      3042359 
1 gross_profit   twitter   2018      2077362 
2 net_income     twitter   2018      1205596 
3 total_revenue  facebook  2018      55838000
4 gross_profit   facebook  2018      46483000
5 net_income     facebook  2018      22112000
6 total_revenue  twitter   2017      2443299 
7 gross_profit   twitter   2017      1582057 
8 net_income     twitter   2017      -108063
การรวมข้อมูลด้วย pandas

การใช้ melt พร้อมกำหนดชื่อคอลัมน์

social_fin_tall = social_fin.melt(id_vars=['financial','company'], 
                                  value_vars=['2018','2017'],
                                  var_name='year', value_name='dollars')
print(social_fin_tall.head(8))
  financial      company   year  dollars 
0 total_revenue  twitter   2018  3042359 
1 gross_profit   twitter   2018  2077362 
2 net_income     twitter   2018  1205596 
3 total_revenue  facebook  2018  55838000
4 gross_profit   facebook  2018  46483000
5 net_income     facebook  2018  22112000
6 total_revenue  twitter   2017  2443299 
7 gross_profit   twitter   2017  1582057
การรวมข้อมูลด้วย pandas

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

การรวมข้อมูลด้วย pandas

Preparing Video For Download...