MCPcopy
hub / github.com/pandas-dev/pandas / drop

Method drop

pandas/core/indexes/multi.py:2765–2871  ·  view source on GitHub ↗

Make a new :class:`pandas.MultiIndex` with the passed list of codes deleted. This method allows for the removal of specified labels from a MultiIndex. The labels to be removed can be provided as a list of tuples if no level is specified, or as a list of labels from

(  # type: ignore[override]
        self,
        codes,
        level: Index | np.ndarray | Iterable[Hashable] | None = None,
        errors: IgnoreRaise = "raise",
    )

Source from the content-addressed store, hash-verified

2763
2764 # error: Signature of "drop" incompatible with supertype "Index"
2765 def drop( # type: ignore[override]
2766 self,
2767 codes,
2768 level: Index | np.ndarray | Iterable[Hashable] | None = None,
2769 errors: IgnoreRaise = "raise",
2770 ) -> MultiIndex:
2771 """
2772 Make a new :class:`pandas.MultiIndex` with the passed list of codes deleted.
2773
2774 This method allows for the removal of specified labels from a MultiIndex.
2775 The labels to be removed can be provided as a list of tuples if no level
2776 is specified, or as a list of labels from a specific level if the level
2777 parameter is provided. This can be useful for refining the structure of a
2778 MultiIndex to fit specific requirements.
2779
2780 Parameters
2781 ----------
2782 codes : array-like
2783 Must be a list of tuples when ``level`` is not specified.
2784 level : int or level name, default None
2785 Level from which the labels will be dropped.
2786 errors : str, default 'raise'
2787 If 'ignore', suppress error and existing labels are dropped.
2788
2789 Returns
2790 -------
2791 MultiIndex
2792 A new MultiIndex with the specified labels removed.
2793
2794 See Also
2795 --------
2796 MultiIndex.remove_unused_levels : Create new MultiIndex from current that
2797 removes unused levels.
2798 MultiIndex.reorder_levels : Rearrange levels using input order.
2799 MultiIndex.rename : Rename levels in a MultiIndex.
2800
2801 Examples
2802 --------
2803 >>> idx = pd.MultiIndex.from_product(
2804 ... [(0, 1, 2), ("green", "purple")], names=["number", "color"]
2805 ... )
2806 >>> idx
2807 MultiIndex([(0, 'green'),
2808 (0, 'purple'),
2809 (1, 'green'),
2810 (1, 'purple'),
2811 (2, 'green'),
2812 (2, 'purple')],
2813 names=['number', 'color'])
2814 >>> idx.drop([(1, "green"), (2, "purple")])
2815 MultiIndex([(0, 'green'),
2816 (0, 'purple'),
2817 (1, 'purple'),
2818 (2, 'green')],
2819 names=['number', 'color'])
2820
2821 We can also drop from a specific level.
2822

Callers 1

test_get_loc_levelMethod · 0.95

Calls 8

_drop_from_levelMethod · 0.95
get_locMethod · 0.95
deleteMethod · 0.95
get_optionFunction · 0.90
find_stack_levelFunction · 0.90
nonzeroMethod · 0.80
dtypeMethod · 0.45
appendMethod · 0.45

Tested by 1

test_get_loc_levelMethod · 0.76