.melt()로 데이터 재구조화하기

pandas로 데이터 조인하기

Aaren Stubberfield

Instructor

와이드 데이터와 롱 데이터

와이드 형식 데이터의 와이드 형식 더 보기

롱 형식 롱 형식의 데이터

pandas로 데이터 조인하기

.melt() 메소드는 무엇을 하나요?

  • melt를 사용하면 데이터세트를 언피벗할 수 있음 피벗 해제 이미지
pandas로 데이터 조인하기

와이드 형식의 데이터세트

이 테이블의 이름은 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로 데이터 조인하기

value_vars와 함께 melt하기

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...