MulDataFrame.melt#

MulDataFrame.melt(prefix=None, value_name='value', ignore_primary_index=False, ignore_primary_columns=False)#

Melt the MulDataFrame into a flattened “records” table.

In the “records” table, each value in the values dataframe occupies a row in which its corresponding metadata in the index and columns dataframes are also filled. The “records” table is a pandas.DataFrame.

Check md.pivot_table for a reverse operation.

Parameters#

prefixNone, True or function

Whether to add prefixes to the common column names in the index and the columns dataframes.

  • None : do not add prefixes. In the “records” table there might be the same columns names coming from the index and the columns dataframe.

  • True : if two names are the same, add 'x_' in front of the name if it comes from the index dataframe and 'y_' if from the columns dataframe.

  • function : a function to customize the prefixes. It is in the signature of def prefix(indexType: 'index'|'columns', name: str) -> str. The first argumnet determines where the column name comes from. For example, if it is 'index', the name is a column name from the index dataframe.

value_namestr, default “value”

How to name the column that contains the values from the values dataframe.

ignore_primary_indexbool, default False

Whether to include the primary index as a column in the “records” table.

ignore_primary_columnsbool, default False

Whether to include the primary columns as a column in the “records” table.

Returns#

pandas.DataFrame

A pandas.DataFrame is returned.

Examples#

>>> import pandas as pd
>>> import muldataframe as md
>>> index = pd.DataFrame([[1,2],[3,6],[5,6]],
             index=['a','b','b'],
             columns=['x','y'])
>>> columns = pd.DataFrame([[5,7],[3,6]],
                index=['c','d'],
                columns=['f','g'])
>>> mf = MulDataFrame([[1,2],[8,9],[8,10]],index=index,columns=columns)
>>> mf.melt()
  index  x  y index  f  g value
0     a  1  2     c  5  7     1
1     a  1  2     d  3  6     2
2     b  3  6     c  5  7     8
3     b  3  6     d  3  6     9
4     b  5  6     c  5  7     8
5     b  5  6     d  3  6    10
>>> mf.melt(prefix=True,value_name='num').columns.tolist()
['x_index','x','y','y_index','f','g','num']