def lreshape(data, groups, dropna=True, label=None):
if isinstance(groups, dict):<EOL><INDENT>keys = list(groups.keys())<EOL>values = list(groups.values())<EOL><DEDENT>else:<EOL><INDENT>keys, values = zip(*groups)<EOL><DEDENT>all_cols = list(set.union(*[set(x) for x in values]))<EOL>id_cols = list(data.columns.difference(all_cols))<EOL>K = len(values[<NUM_LIT:0>])<EOL>for seq in values:<EOL><INDENT>if len(seq) != K:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT><DEDENT>mdata = {}<EOL>pivot_cols = []<EOL>for target, names in zip(keys, values):<EOL><INDENT>to_concat = [data[col].values for col in names]<EOL>import pandas.core.dtypes.concat as _concat<EOL>mdata[target] = _concat._concat_compat(to_concat)<EOL>pivot_cols.append(target)<EOL><DEDENT>for col in id_cols:<EOL><INDENT>mdata[col] = np.tile(data[col].values, K)<EOL><DEDENT>if dropna:<EOL><INDENT>mask = np.ones(len(mdata[pivot_cols[<NUM_LIT:0>]]), dtype=bool)<EOL>for c in pivot_cols:<EOL><INDENT>mask &= notna(mdata[c])<EOL><DEDENT>if not mask.all():<EOL><INDENT>mdata = {k: v[mask] for k, v in mdata.items()}<EOL><DEDENT><DEDENT>return data._constructor(mdata, columns=id_cols + pivot_cols)<EOL>
Reshape long-format data to wide. Generalized inverse of DataFrame.pivot

Parameters
----------
data : DataFrame
groups : dict
    {new_name : list_of_columns}
dropna : boolean, default True

Examples
--------
>>> data = pd.DataFrame({'hr1': [514, 573], 'hr2': [545, 526],
...                      'team': ['Red Sox', 'Yankees'],
...                      'year1': [2007, 2007], 'year2': [2008, 2008]})
>>> data
   hr1  hr2     team  year1  year2
0  514  545  Red Sox   2007   2008
1  573  526  Yankees   2007   2008

>>> pd.lreshape(data, {'year': ['year1', 'year2'], 'hr': ['hr1', 'hr2']})
      team  year   hr
0  Red Sox  2007  514
1  Yankees  2007  573
2  Red Sox  2008  545
3  Yankees  2008  526

Returns
-------
reshaped : DataFrame