Skip to content

Commit d96ff29

Browse files
stijnvanhoeyjreback
authored andcommitted
DOC: Make example running example
closes #15624
1 parent 66fb0a3 commit d96ff29

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

pandas/core/groupby.py

+49-9
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,33 @@
109109
110110
Examples
111111
--------
112-
>>> df = pd.DataFrame(np.repeat(np.arange(10), 3).reshape(-1, 3),
113-
columns=list('ABC'))
114-
>>> grouped = df.groupby(df.index // 3)
115112
116113
# Same shape
117-
>>> grouped.%(selected)stransform(lambda x: (x - x.mean()) / x.std())
114+
>>> df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
115+
... 'foo', 'bar'],
116+
... 'B' : ['one', 'one', 'two', 'three',
117+
... 'two', 'two'],
118+
... 'C' : [1, 5, 5, 2, 5, 5],
119+
... 'D' : [2.0, 5., 8., 1., 2., 9.]})
120+
>>> grouped = df.groupby('A')
121+
>>> grouped.transform(lambda x: (x - x.mean()) / x.std())
122+
C D
123+
0 -1.154701 -0.577350
124+
1 0.577350 0.000000
125+
2 0.577350 1.154701
126+
3 -1.154701 -1.000000
127+
4 0.577350 -0.577350
128+
5 0.577350 1.000000
118129
119130
# Broadcastable
120-
>>> grouped.%(selected)stransform(lambda x: x.max() - x.min())
131+
>>> grouped.transform(lambda x: x.max() - x.min())
132+
C D
133+
0 4 6.0
134+
1 3 8.0
135+
2 4 6.0
136+
3 3 8.0
137+
4 4 6.0
138+
5 3 8.0
121139
122140
"""
123141

@@ -2982,7 +3000,17 @@ def filter(self, func, dropna=True, *args, **kwargs): # noqa
29823000
29833001
Examples
29843002
--------
2985-
>>> grouped.filter(lambda x: x.mean() > 0)
3003+
>>> import pandas as pd
3004+
>>> df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
3005+
... 'foo', 'bar'],
3006+
... 'B' : [1, 2, 3, 4, 5, 6],
3007+
... 'C' : [2.0, 5., 8., 1., 2., 9.]})
3008+
>>> grouped = df.groupby('A')
3009+
>>> df.groupby('A').B.filter(lambda x: x.mean() > 3.)
3010+
1 2
3011+
3 4
3012+
5 6
3013+
Name: B, dtype: int64
29863014
29873015
Returns
29883016
-------
@@ -3784,9 +3812,21 @@ def filter(self, func, dropna=True, *args, **kwargs): # noqa
37843812
37853813
Examples
37863814
--------
3787-
>>> df = pd.DataFrame(np.random.randn(10, 3), columns=list('ABC'))
3788-
>>> grouped = df.groupby(df.index % 3)
3789-
>>> grouped.filter(lambda x: x['A'].sum() + x['B'].sum() > 0)
3815+
>>> import pandas as pd
3816+
>>> df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
3817+
... 'foo', 'bar'],
3818+
... 'B' : [1, 2, 3, 4, 5, 6],
3819+
... 'C' : [2.0, 5., 8., 1., 2., 9.]})
3820+
>>> grouped = df.groupby('A')
3821+
>>> grouped.filter(lambda x: x['B'].mean() > 3.)
3822+
A B C
3823+
1 bar 2 5.0
3824+
3 bar 4 1.0
3825+
5 bar 6 9.0
3826+
3827+
Returns
3828+
-------
3829+
filtered : DataFrame
37903830
"""
37913831

37923832
indices = []

0 commit comments

Comments
 (0)