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",
)
| 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 |