Advertisement
elena1234

melt and pivot dataframe in Python

Mar 16th, 2023 (edited)
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. # from short to long format
  2.  
  3. table_2012.melt(id_vars = "Country", value_vars = ["Gold", "Silver", "Bronze"], var_name="Medal", value_name="Count")
  4.  
  5. pivot.melt(id_vars= 'model_year', value_vars= ['europe', 'japan', 'usa'], var_name = 'origin', value_name = 'mpg')
  6.  
  7. #################################################################################################################
  8. # from long to short format
  9.  
  10. melt_2012.pivot(index = "Country", columns = "Medal", values = "Count").sort_values("Gold", ascending = False)
  11.  
  12. pivot = mean_mpg.pivot(index = "model_year", columns = "origin", values = "mpg")
  13.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement