|
11 | 11 | import numpy as np
|
12 | 12 |
|
13 | 13 | from pandas._libs import lib
|
14 |
| -from pandas.util._decorators import ( |
15 |
| - Appender, |
16 |
| - Substitution, |
17 |
| -) |
18 | 14 |
|
19 | 15 | from pandas.core.dtypes.cast import maybe_downcast_to_dtype
|
20 | 16 | from pandas.core.dtypes.common import (
|
|
29 | 25 | )
|
30 | 26 |
|
31 | 27 | import pandas.core.common as com
|
32 |
| -from pandas.core.frame import _shared_docs |
33 | 28 | from pandas.core.groupby import Grouper
|
34 | 29 | from pandas.core.indexes.api import (
|
35 | 30 | Index,
|
@@ -656,15 +651,159 @@ def _convert_by(by):
|
656 | 651 | return by
|
657 | 652 |
|
658 | 653 |
|
659 |
| -@Substitution("\ndata : DataFrame") |
660 |
| -@Appender(_shared_docs["pivot"], indents=1) |
661 | 654 | def pivot(
|
662 | 655 | data: DataFrame,
|
663 | 656 | *,
|
664 | 657 | columns: IndexLabel,
|
665 | 658 | index: IndexLabel | lib.NoDefault = lib.no_default,
|
666 | 659 | values: IndexLabel | lib.NoDefault = lib.no_default,
|
667 | 660 | ) -> DataFrame:
|
| 661 | + """ |
| 662 | + Return reshaped DataFrame organized by given index / column values. |
| 663 | +
|
| 664 | + Reshape data (produce a "pivot" table) based on column values. Uses |
| 665 | + unique values from specified `index` / `columns` to form axes of the |
| 666 | + resulting DataFrame. This function does not support data |
| 667 | + aggregation, multiple values will result in a MultiIndex in the |
| 668 | + columns. See the :ref:`User Guide <reshaping>` for more on reshaping. |
| 669 | +
|
| 670 | + Parameters |
| 671 | + ---------- |
| 672 | + data : DataFrame |
| 673 | + Input pandas DataFrame object. |
| 674 | + columns : str or object or a list of str |
| 675 | + Column to use to make new frame's columns. |
| 676 | + index : str or object or a list of str, optional |
| 677 | + Column to use to make new frame's index. If not given, uses existing index. |
| 678 | + values : str, object or a list of the previous, optional |
| 679 | + Column(s) to use for populating new frame's values. If not |
| 680 | + specified, all remaining columns will be used and the result will |
| 681 | + have hierarchically indexed columns. |
| 682 | +
|
| 683 | + Returns |
| 684 | + ------- |
| 685 | + DataFrame |
| 686 | + Returns reshaped DataFrame. |
| 687 | +
|
| 688 | + Raises |
| 689 | + ------ |
| 690 | + ValueError: |
| 691 | + When there are any `index`, `columns` combinations with multiple |
| 692 | + values. `DataFrame.pivot_table` when you need to aggregate. |
| 693 | +
|
| 694 | + See Also |
| 695 | + -------- |
| 696 | + DataFrame.pivot_table : Generalization of pivot that can handle |
| 697 | + duplicate values for one index/column pair. |
| 698 | + DataFrame.unstack : Pivot based on the index values instead of a |
| 699 | + column. |
| 700 | + wide_to_long : Wide panel to long format. Less flexible but more |
| 701 | + user-friendly than melt. |
| 702 | +
|
| 703 | + Notes |
| 704 | + ----- |
| 705 | + For finer-tuned control, see hierarchical indexing documentation along |
| 706 | + with the related stack/unstack methods. |
| 707 | +
|
| 708 | + Reference :ref:`the user guide <reshaping.pivot>` for more examples. |
| 709 | +
|
| 710 | + Examples |
| 711 | + -------- |
| 712 | + >>> df = pd.DataFrame( |
| 713 | + ... { |
| 714 | + ... "foo": ["one", "one", "one", "two", "two", "two"], |
| 715 | + ... "bar": ["A", "B", "C", "A", "B", "C"], |
| 716 | + ... "baz": [1, 2, 3, 4, 5, 6], |
| 717 | + ... "zoo": ["x", "y", "z", "q", "w", "t"], |
| 718 | + ... } |
| 719 | + ... ) |
| 720 | + >>> df |
| 721 | + foo bar baz zoo |
| 722 | + 0 one A 1 x |
| 723 | + 1 one B 2 y |
| 724 | + 2 one C 3 z |
| 725 | + 3 two A 4 q |
| 726 | + 4 two B 5 w |
| 727 | + 5 two C 6 t |
| 728 | +
|
| 729 | + >>> df.pivot(index="foo", columns="bar", values="baz") |
| 730 | + bar A B C |
| 731 | + foo |
| 732 | + one 1 2 3 |
| 733 | + two 4 5 6 |
| 734 | +
|
| 735 | + >>> df.pivot(index="foo", columns="bar")["baz"] |
| 736 | + bar A B C |
| 737 | + foo |
| 738 | + one 1 2 3 |
| 739 | + two 4 5 6 |
| 740 | +
|
| 741 | + >>> df.pivot(index="foo", columns="bar", values=["baz", "zoo"]) |
| 742 | + baz zoo |
| 743 | + bar A B C A B C |
| 744 | + foo |
| 745 | + one 1 2 3 x y z |
| 746 | + two 4 5 6 q w t |
| 747 | +
|
| 748 | + You could also assign a list of column names or a list of index names. |
| 749 | +
|
| 750 | + >>> df = pd.DataFrame( |
| 751 | + ... { |
| 752 | + ... "lev1": [1, 1, 1, 2, 2, 2], |
| 753 | + ... "lev2": [1, 1, 2, 1, 1, 2], |
| 754 | + ... "lev3": [1, 2, 1, 2, 1, 2], |
| 755 | + ... "lev4": [1, 2, 3, 4, 5, 6], |
| 756 | + ... "values": [0, 1, 2, 3, 4, 5], |
| 757 | + ... } |
| 758 | + ... ) |
| 759 | + >>> df |
| 760 | + lev1 lev2 lev3 lev4 values |
| 761 | + 0 1 1 1 1 0 |
| 762 | + 1 1 1 2 2 1 |
| 763 | + 2 1 2 1 3 2 |
| 764 | + 3 2 1 2 4 3 |
| 765 | + 4 2 1 1 5 4 |
| 766 | + 5 2 2 2 6 5 |
| 767 | +
|
| 768 | + >>> df.pivot(index="lev1", columns=["lev2", "lev3"], values="values") |
| 769 | + lev2 1 2 |
| 770 | + lev3 1 2 1 2 |
| 771 | + lev1 |
| 772 | + 1 0.0 1.0 2.0 NaN |
| 773 | + 2 4.0 3.0 NaN 5.0 |
| 774 | +
|
| 775 | + >>> df.pivot(index=["lev1", "lev2"], columns=["lev3"], values="values") |
| 776 | + lev3 1 2 |
| 777 | + lev1 lev2 |
| 778 | + 1 1 0.0 1.0 |
| 779 | + 2 2.0 NaN |
| 780 | + 2 1 4.0 3.0 |
| 781 | + 2 NaN 5.0 |
| 782 | +
|
| 783 | + A ValueError is raised if there are any duplicates. |
| 784 | +
|
| 785 | + >>> df = pd.DataFrame( |
| 786 | + ... { |
| 787 | + ... "foo": ["one", "one", "two", "two"], |
| 788 | + ... "bar": ["A", "A", "B", "C"], |
| 789 | + ... "baz": [1, 2, 3, 4], |
| 790 | + ... } |
| 791 | + ... ) |
| 792 | + >>> df |
| 793 | + foo bar baz |
| 794 | + 0 one A 1 |
| 795 | + 1 one A 2 |
| 796 | + 2 two B 3 |
| 797 | + 3 two C 4 |
| 798 | +
|
| 799 | + Notice that the first two rows are the same for our `index` |
| 800 | + and `columns` arguments. |
| 801 | +
|
| 802 | + >>> df.pivot(index="foo", columns="bar", values="baz") |
| 803 | + Traceback (most recent call last): |
| 804 | + ... |
| 805 | + ValueError: Index contains duplicate entries, cannot reshape |
| 806 | + """ |
668 | 807 | columns_listlike = com.convert_to_list_like(columns)
|
669 | 808 |
|
670 | 809 | # If columns is None we will create a MultiIndex level with None as name
|
|
0 commit comments