9951 explained code solutions for 126 technologies


python-pandasHow to use melt() with dataframe - example


import pandas as pd

df = pd.DataFrame({
  'Id':    [1, 10, 12, 15, 34],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Phone Price': [204, 304, 404, 405, 305]
})

melted = pd.melt(df, id_vars =['Phone'], value_vars =['Phone Price'])ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

.melt(

transofrms given dataframe from pivot form (index/column/value) to "wide" form (index/var-name/var-value)

id_vars =['Phone']

columns to use as index

value_vars =['Phone Price']

column to use for variable/value rows


Usage example

import pandas as pd

df = pd.DataFrame({
  'Id':    [1, 10, 12, 15, 34],
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Phone Price': [204, 304, 404, 405, 305]
})

melted = pd.melt(df, id_vars =['Phone'], value_vars =['Phone Price'])

print(melted)
output
  Phone     variable  value
0   ip5  Phone Price    204
1   ip6  Phone Price    304
2   ip8  Phone Price    404
3   sms  Phone Price    405
4    xi  Phone Price    305