Joining Data with pandas
Aaren Stubberfield
Instructor
Wide Format

Long Format


This table is called 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
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
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
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
Joining Data with pandas