Skip to content

Commit 3b5d101

Browse files
DOC: seperate drop doc for series and dataframes
1 parent 9119d07 commit 3b5d101

File tree

3 files changed

+215
-67
lines changed

3 files changed

+215
-67
lines changed

pandas/core/frame.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,6 +3035,128 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
30353035
method=method, level=level, copy=copy,
30363036
limit=limit, fill_value=fill_value)
30373037

3038+
def drop(self, labels=None, axis=0, index=None, columns=None,
3039+
level=None, inplace=False, errors='raise'):
3040+
"""
3041+
Drop rows or columns.
3042+
3043+
Remove rows or columns by specifying label names and corresponding axis,
3044+
or by specifying directly index or column names. When using a
3045+
multi-index, labels on different levels can be removed by specifying
3046+
the level name or int.
3047+
3048+
Parameters
3049+
----------
3050+
labels : single label or list-like
3051+
Index or column labels to drop.
3052+
axis : int or axis name, default 0
3053+
Whether to drop labels from the index (0 / 'index') or
3054+
columns (1 / 'columns').
3055+
index : None
3056+
Redundant for application on Series, but index can be used instead
3057+
of labels.
3058+
columns : None
3059+
Redundant for application on Series, but index can be used instead
3060+
of labels.
3061+
3062+
.. versionadded:: 0.21.0
3063+
level : int or level name, optional
3064+
For MultiIndex.
3065+
inplace : bool, default False
3066+
If True, do operation inplace and return None.
3067+
errors : {'ignore', 'raise'}, default 'raise'
3068+
If 'ignore', suppress error and existing labels are dropped.
3069+
3070+
Returns
3071+
-------
3072+
dropped : type of caller
3073+
3074+
See Also
3075+
--------
3076+
DataFrame.dropna : Return DataFrame with labels on given axis omitted
3077+
where (all or any) data are missing
3078+
DataFrame.drop_duplicates : Return DataFrame with duplicate rows removed,
3079+
optionally only considering certain columns
3080+
3081+
Raises
3082+
------
3083+
KeyError
3084+
If none of the labels are found in the selected axis
3085+
3086+
Examples
3087+
--------
3088+
>>> df = pd.DataFrame(np.arange(12).reshape(3,4),
3089+
... columns=['A', 'B', 'C', 'D'])
3090+
>>> df
3091+
A B C D
3092+
0 0 1 2 3
3093+
1 4 5 6 7
3094+
2 8 9 10 11
3095+
3096+
Drop columns
3097+
3098+
>>> df.drop(['B', 'C'], axis=1)
3099+
A D
3100+
0 0 3
3101+
1 4 7
3102+
2 8 11
3103+
3104+
>>> df.drop(columns=['B', 'C'])
3105+
A D
3106+
0 0 3
3107+
1 4 7
3108+
2 8 11
3109+
3110+
Drop a row by index
3111+
3112+
>>> df.drop([0, 1])
3113+
A B C D
3114+
2 8 9 10 11
3115+
3116+
Drop columns and/or rows of MultiIndex
3117+
3118+
>>> midx = pd.MultiIndex(levels=[['lama','cow','falcon'],
3119+
... ['speed','weight','length']],
3120+
... labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
3121+
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
3122+
>>> df = pd.DataFrame(index=midx, columns=['big','small'],
3123+
... data=[[45,30],[200,100],[1.5,1],[30,20],[250,150],
3124+
... [1.5,0.8],[320,250],[1,0.8],[0.3,0.2]])
3125+
>>> df
3126+
big small
3127+
lama speed 45.0 30.0
3128+
weight 200.0 100.0
3129+
length 1.5 1.0
3130+
cow speed 30.0 20.0
3131+
weight 250.0 150.0
3132+
length 1.5 0.8
3133+
falcon speed 320.0 250.0
3134+
weight 1.0 0.8
3135+
length 0.3 0.2
3136+
3137+
>>> df.drop(index='cow',columns='small')
3138+
big
3139+
lama speed 45.0
3140+
weight 200.0
3141+
length 1.5
3142+
falcon speed 320.0
3143+
weight 1.0
3144+
length 0.3
3145+
3146+
>>> df.drop(index='length', level=1)
3147+
big small
3148+
lama speed 45.0 30.0
3149+
weight 200.0 100.0
3150+
cow speed 30.0 20.0
3151+
weight 250.0 150.0
3152+
falcon speed 320.0 250.0
3153+
weight 1.0 0.8
3154+
"""
3155+
return super(DataFrame,
3156+
self).drop(labels=labels, axis=axis, index=index,
3157+
columns=columns, level=level, inplace=inplace,
3158+
errors=errors)
3159+
30383160
@rewrite_axis_style_signature('mapper', [('copy', True),
30393161
('inplace', False),
30403162
('level', None)])

pandas/core/generic.py

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,75 +2797,10 @@ def reindex_like(self, other, method=None, copy=True, limit=None,
27972797

27982798
return self.reindex(**d)
27992799

2800+
28002801
def drop(self, labels=None, axis=0, index=None, columns=None, level=None,
28012802
inplace=False, errors='raise'):
2802-
"""
2803-
Return new object with labels in requested axis removed.
2804-
2805-
Parameters
2806-
----------
2807-
labels : single label or list-like
2808-
Index or column labels to drop.
2809-
axis : int or axis name
2810-
Whether to drop labels from the index (0 / 'index') or
2811-
columns (1 / 'columns').
2812-
index, columns : single label or list-like
2813-
Alternative to specifying `axis` (``labels, axis=1`` is
2814-
equivalent to ``columns=labels``).
2815-
2816-
.. versionadded:: 0.21.0
2817-
level : int or level name, default None
2818-
For MultiIndex
2819-
inplace : bool, default False
2820-
If True, do operation inplace and return None.
2821-
errors : {'ignore', 'raise'}, default 'raise'
2822-
If 'ignore', suppress error and existing labels are dropped.
2823-
2824-
Returns
2825-
-------
2826-
dropped : type of caller
2827-
2828-
Raises
2829-
------
2830-
KeyError
2831-
If none of the labels are found in the selected axis
2832-
2833-
Examples
2834-
--------
2835-
>>> df = pd.DataFrame(np.arange(12).reshape(3,4),
2836-
columns=['A', 'B', 'C', 'D'])
2837-
>>> df
2838-
A B C D
2839-
0 0 1 2 3
2840-
1 4 5 6 7
2841-
2 8 9 10 11
2842-
2843-
Drop columns
2844-
2845-
>>> df.drop(['B', 'C'], axis=1)
2846-
A D
2847-
0 0 3
2848-
1 4 7
2849-
2 8 11
2850-
2851-
>>> df.drop(columns=['B', 'C'])
2852-
A D
2853-
0 0 3
2854-
1 4 7
2855-
2 8 11
2856-
2857-
Drop a row by index
2858-
2859-
>>> df.drop([0, 1])
2860-
A B C D
2861-
2 8 9 10 11
2862-
2863-
Notes
2864-
-----
2865-
Specifying both `labels` and `index` or `columns` will raise a
2866-
ValueError.
2867-
2868-
"""
2803+
28692804
inplace = validate_bool_kwarg(inplace, 'inplace')
28702805

28712806
if labels is not None:

pandas/core/series.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,97 @@ def rename(self, index=None, **kwargs):
26602660
def reindex(self, index=None, **kwargs):
26612661
return super(Series, self).reindex(index=index, **kwargs)
26622662

2663+
def drop(self, labels=None, axis=0, index=None, columns=None,
2664+
level=None, inplace=False, errors='raise'):
2665+
"""
2666+
Return Series with specified index labels removed.
2667+
2668+
Remove elements of a Series based on specifying the index labels.
2669+
When using a multi-index, labels on different levels can be removed
2670+
by specifying the level name or int.
2671+
2672+
Parameters
2673+
----------
2674+
labels : single label or list-like
2675+
Index labels to drop.
2676+
axis : 0, default 0
2677+
Redundant for application on Series.
2678+
index : None
2679+
Redundant for application on Series, but index can be used instead
2680+
of labels.
2681+
columns : None
2682+
Redundant for application on Series, but index can be used instead
2683+
of labels.
2684+
.. versionadded:: 0.21.0
2685+
level : int or level name, optional
2686+
For MultiIndex.
2687+
inplace : bool, default False
2688+
If True, do operation inplace and return None.
2689+
errors : {'ignore', 'raise'}, default 'raise'
2690+
If 'ignore', suppress error and existing labels are dropped.
2691+
2692+
Returns
2693+
-------
2694+
dropped : type of caller
2695+
2696+
See Also
2697+
--------
2698+
Series.reindex : Return only specified index labels of Series.
2699+
Series.dropna : Return series without null values.
2700+
Series.drop_duplicates : Return Series with duplicate values removed.
2701+
2702+
Raises
2703+
------
2704+
KeyError
2705+
If none of the labels are found in the index.
2706+
2707+
Examples
2708+
--------
2709+
>>> s = pd.Series(data=np.arange(3), index=['A','B','C'])
2710+
>>> s
2711+
A 0
2712+
B 1
2713+
C 2
2714+
dtype: int64
2715+
2716+
Drop labels B en C
2717+
2718+
>>> s.drop(labels=['B','C'])
2719+
A 0
2720+
dtype: int64
2721+
2722+
Drop 2nd level label in MultiIndex Series
2723+
2724+
>>> midx = pd.MultiIndex(levels=[['lama','cow','falcon'],
2725+
... ['speed','weight','length']],
2726+
... labels=[[0, 0, 0, 1, 1, 1, 2, 2, 2],
2727+
... [0, 1, 2, 0, 1, 2, 0, 1, 2]])
2728+
>>> s = pd.Series(data=[45,200,1.2,30,250,1.5,320,1,0.3], index=midx)
2729+
>>> s
2730+
lama speed 45.0
2731+
weight 200.0
2732+
length 1.2
2733+
cow speed 30.0
2734+
weight 250.0
2735+
length 1.5
2736+
falcon speed 320.0
2737+
weight 1.0
2738+
length 0.3
2739+
dtype: float64
2740+
2741+
>>> s.drop(labels='weight', level=1)
2742+
lama speed 45.0
2743+
length 1.2
2744+
cow speed 30.0
2745+
length 1.5
2746+
falcon speed 320.0
2747+
length 0.3
2748+
dtype: float64
2749+
"""
2750+
return super(Series, self).drop(labels=labels, axis=axis, index=index,
2751+
columns=columns, level=level,
2752+
inplace=inplace, errors=errors)
2753+
26632754
@Appender(generic._shared_docs['fillna'] % _shared_doc_kwargs)
26642755
def fillna(self, value=None, method=None, axis=None, inplace=False,
26652756
limit=None, downcast=None, **kwargs):

0 commit comments

Comments
 (0)