Pandas. Dataframe.unstack

Source: Internet
Author: User
Official documents: Pandas. Dataframe.unstack¶Dataframe. Unstack (Level=-1, fill_value=none) [source]¶

Pivot A level of the (necessarily hierarchical) index labels, returning a DATAFRAME has a new level of column labels WH OSE Inner-most level consists of the pivoted index labels. If The index is not a multiindex, the output would be a Series (the analogue's stack when the columns are not a multiindex (when there is only one row index, the result generates a series, which is detailed in the following example) the level involved'll automatically get sorted.

Parameters:

Level: int, string, or List of the default-1 (last level)

Level (s) of the index to Unstack, can pass level name

fill_value : Replace NaN with this value if the unstack produces

Missing values

Returns:

unstacked : Dataframe or Series

Also dataframe.pivot pivot a table based on column values. Dataframe.stack Pivot A level of the column labels (inverse operation from Unstack).

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

The series returned here would have been: one     two
a  b       a  b
1  2       3  4

Such a dateframe, the same is to convert the row index to the column index, only so that the data is only one row, so automatically converted to series.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.