Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les compléments
Voir le programme détaillé
Classe « DataFrame »

Méthode pandas.DataFrame.unstack

Signature de la méthode unstack

def unstack(self, level: 'IndexLabel' = -1, fill_value=None, sort: 'bool' = True) 

Description

help(DataFrame.unstack)

Pivot a level of the (necessarily hierarchical) index labels.

Returns a DataFrame having a new level of column labels whose inner-most level
consists of the pivoted index labels.

If the index is not a MultiIndex, the output will be a Series
(the analogue of stack when the columns are not a MultiIndex).

Parameters
----------
level : int, str, or list of these, default -1 (last level)
    Level(s) of index to unstack, can pass level name.
fill_value : int, str or dict
    Replace NaN with this value if the unstack produces missing values.
sort : bool, default True
    Sort the level(s) in the resulting MultiIndex columns.

Returns
-------
Series or DataFrame

See Also
--------
DataFrame.pivot : Pivot a table based on column values.
DataFrame.stack : Pivot a level of the column labels (inverse operation
    from `unstack`).

Notes
-----
Reference :ref:`the user guide <reshaping.stacking>` for more examples.

Examples
--------
>>> index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'),
...                                    ('two', 'a'), ('two', 'b')])
>>> s = pd.Series(np.arange(1.0, 5.0), index=index)
>>> s
one  a   1.0
     b   2.0
two  a   3.0
     b   4.0
dtype: float64

>>> s.unstack(level=-1)
     a   b
one  1.0  2.0
two  3.0  4.0

>>> s.unstack(level=0)
   one  two
a  1.0   3.0
b  2.0   4.0

>>> df = s.unstack(level=0)
>>> df.unstack()
one  a  1.0
     b  2.0
two  a  3.0
     b  4.0
dtype: float64


Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les compléments
Voir le programme détaillé