|
109 | 109 |
|
110 | 110 | Examples
|
111 | 111 | --------
|
112 |
| ->>> df = pd.DataFrame(np.repeat(np.arange(10), 3).reshape(-1, 3), |
113 |
| - columns=list('ABC')) |
114 |
| ->>> grouped = df.groupby(df.index // 3) |
115 | 112 |
|
116 | 113 | # 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 |
118 | 129 |
|
119 | 130 | # 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 |
121 | 139 |
|
122 | 140 | """
|
123 | 141 |
|
@@ -2982,7 +3000,17 @@ def filter(self, func, dropna=True, *args, **kwargs): # noqa
|
2982 | 3000 |
|
2983 | 3001 | Examples
|
2984 | 3002 | --------
|
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 |
2986 | 3014 |
|
2987 | 3015 | Returns
|
2988 | 3016 | -------
|
@@ -3784,9 +3812,21 @@ def filter(self, func, dropna=True, *args, **kwargs): # noqa
|
3784 | 3812 |
|
3785 | 3813 | Examples
|
3786 | 3814 | --------
|
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 |
3790 | 3830 | """
|
3791 | 3831 |
|
3792 | 3832 | indices = []
|
|
0 commit comments