Skip to content

Instantly share code, notes, and snippets.

@sanidhya-singh
Created June 11, 2020 16:08
Show Gist options
  • Save sanidhya-singh/6ded4d42ffd8e89014d16e01ab511435 to your computer and use it in GitHub Desktop.
Save sanidhya-singh/6ded4d42ffd8e89014d16e01ab511435 to your computer and use it in GitHub Desktop.
Python melt function
import pandas as pd
data = [["a", 11, 12, 13], ["b", 21, 22, 23]]
df = pd.DataFrame(data)
df.columns = ["company", "water", "elec", "rent"]
# display(df)
new_df = pd.melt(df, id_vars=['company'], value_vars=['water', 'elec', 'rent'])
new_df.columns = ["Company", "Bills", "Amount"]
display(new_df.sort_values(by=['Company']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment