Created
June 11, 2020 16:08
-
-
Save sanidhya-singh/6ded4d42ffd8e89014d16e01ab511435 to your computer and use it in GitHub Desktop.
Python melt function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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