@@ -3076,7 +3076,7 @@ from pandas import Series, DataFrame
3076
3076
** Ordered dictionary with a name.**
3077
3077
3078
3078
``` python
3079
- >> > Series([1 , 2 ], index = [' x' , ' y' ], name = ' a' )
3079
+ >> > sr = Series([1 , 2 ], index = [' x' , ' y' ], name = ' a' )
3080
3080
x 1
3081
3081
y 2
3082
3082
Name: a, dtype: int64
@@ -3110,22 +3110,37 @@ Name: a, dtype: int64
3110
3110
< Sr> = < Sr> .combine_first(< Sr> ) # Adds items that are not yet present (extends).
3111
3111
```
3112
3112
3113
- #### Operations :
3113
+ #### Aggregations :
3114
3114
``` python
3115
3115
< el> = < Sr> .sum/ max / mean/ idxmax/ all ()
3116
+ < el> = < agg_func> (< Sr> )
3117
+ < el> = < Sr> .apply/ agg(< agg_func> ) # Apply can only accept strings.
3118
+ ```
3119
+
3120
+ ``` python
3121
+ + ------------ -+ -------- + ---------- -+ -------------- -+
3122
+ | | ' sum' | [' sum' ] | {' s' : ' sum' } |
3123
+ + ------------ -+ -------- + ---------- -+ -------------- -+
3124
+ | sr.apply(…) | | | |
3125
+ | sr.agg(…) | 3 | sum 3 | s 3 |
3126
+ | | | | |
3127
+ + ------------ -+ -------- + ---------- -+ -------------- -+
3128
+ ```
3129
+
3130
+ #### Transformations:
3131
+ ``` python
3116
3132
< Sr> = < Sr> .diff/ cumsum/ rank/ pct_change() # …/fillna/ffill/interpolate()
3117
- < el> = < Sr> .apply/ agg(< agg_func> )
3118
3133
< Sr> = < Sr> .apply/ agg/ transform(< trans_func> )
3119
3134
```
3120
3135
3121
3136
``` python
3122
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + -- -----------+ -------------- -+
3123
- | | ' sum ' | [ ' sum ' ] | { ' s ' : ' sum ' } | ' rank' | [' rank' ] | {' r' : ' rank' } |
3124
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + -- -----------+ -------------- -+
3125
- | sr.apply(…) | | | | | rank | |
3126
- | sr.agg(…) | 3 | sum 3 | s 3 | x 1 | x 1 | r x 1 |
3127
- | | | | | y 2 | y 2 | y 2 |
3128
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + -- -----------+ -------------- -+
3137
+ + ------------ -+ -------- + ---------- -+ -------------- -+
3138
+ | | ' rank' | [' rank' ] | {' r' : ' rank' } |
3139
+ + ------------ -+ -------- + ---------- -+ -------------- -+
3140
+ | sr.apply(…) | | rank | |
3141
+ | sr.agg(…) | x 1 | x 1 | r x 1 |
3142
+ | sr.trans(…) | y 2 | y 2 | y 2 |
3143
+ + ------------ -+ -------- + ---------- -+ -------------- -+
3129
3144
```
3130
3145
3131
3146
### DataFrame
@@ -3168,27 +3183,44 @@ b 3 4
3168
3183
< DF > = < DF > .melt(id_vars = column_key/ s) # Melts on columns.
3169
3184
```
3170
3185
3171
- #### Operations:
3172
3186
``` python
3173
3187
< Sr> = < DF > .sum/ max / mean/ idxmax/ all ()
3174
- < DF > = < DF > .diff/ cumsum/ rank() # …/pct_change/fillna/ffill/interpolate()
3175
3188
< Sr> = < DF > .apply/ agg/ transform(< agg_func> )
3189
+ < DF > = < DF > .diff/ cumsum/ rank() # …/pct_change/fillna/ffill/interpolate()
3176
3190
< DF > = < DF > .apply/ agg/ transform(< trans_func> )
3177
3191
< DF > = < DF > .applymap(< func> ) # Apply a function to a Dataframe elementwise.
3178
3192
```
3179
3193
* ** All operations operate on columns by default. Use ` 'axis=1' ` parameter to process the rows instead.**
3180
3194
3195
+ #### Apply, Aggregate, Transform:
3181
3196
``` python
3182
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + ------------ -+ -------------- -+
3183
- | | ' sum' | [' sum' ] | {' x' : ' sum' } | ' rank' | [' rank' ] | {' x' : ' rank' } |
3184
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + ------------ -+ -------------- -+
3185
- | df.apply(…) | | x y | | x y | x y | x |
3186
- | df.agg(…) | x 4 | sum 4 6 | x 4 | a 1 1 | rank rank | a 1 |
3187
- | df.trans(…) | y 6 | | | b 2 2 | a 1 1 | b 2 |
3188
- | | | | | | b 2 2 | |
3189
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + ------------ -+ -------------- -+
3197
+ >> > df = DataFrame([[1 , 2 ], [3 , 4 ]], index = [' a' , ' b' ], columns = [' x' , ' y' ])
3198
+ x y
3199
+ a 1 2
3200
+ b 3 4
3190
3201
```
3191
- * ** Transform doesen't work with ` ['sum'] ` and ` {'x': 'sum'} ` .**
3202
+
3203
+ ``` python
3204
+ + ------------ -+ -------------- -+ -------------- -+ -------------- -+
3205
+ | | ' sum' | [' sum' ] | {' x' : ' sum' } |
3206
+ + ------------ -+ -------------- -+ -------------- -+ -------------- -+
3207
+ | df.apply(…) | | x y | |
3208
+ | df.agg(…) | x 4 | sum 4 6 | x 4 |
3209
+ | df.trans(…) | y 6 | | |
3210
+ + ------------ -+ -------------- -+ -------------- -+ -------------- -+
3211
+ ```
3212
+
3213
+ ``` python
3214
+ + ------------ -+ -------------- -+ -------------- -+ -------------- -+
3215
+ | | ' rank' | [' rank' ] | {' x' : ' rank' } |
3216
+ + ------------ -+ -------------- -+ -------------- -+ -------------- -+
3217
+ | df.apply(…) | x y | x y | x |
3218
+ | df.agg(…) | a 1 1 | rank rank | a 1 |
3219
+ | df.trans(…) | b 2 2 | a 1 1 | b 2 |
3220
+ | | | b 2 2 | |
3221
+ + ------------ -+ -------------- -+ -------------- -+ -------------- -+
3222
+ ```
3223
+ * ** Transform() doesen't work with ` ['sum'] ` and ` {'x': 'sum'} ` .**
3192
3224
3193
3225
#### Merge, Join, Concat:
3194
3226
``` python
@@ -3253,42 +3285,63 @@ c 6 7
3253
3285
```
3254
3286
* ** Result of an operation is a dataframe with index made up of group keys. Use ` '<DF>.reset_index()' ` to move the index back into it's own column.**
3255
3287
3256
- #### Operations :
3288
+ #### Aggregations :
3257
3289
``` python
3258
3290
< DF > = < GB > .sum/ max / mean/ idxmax/ all ()
3259
- < DF > = < GB > .diff/ cumsum/ rank() # …/pct_change/fillna/ffill()
3260
3291
< DF > = < GB > .apply/ agg/ transform(< agg_func> )
3292
+ ```
3293
+
3294
+ ``` python
3295
+ + ------------ -+ ------------ + ------------ -+ -------------- -+
3296
+ | | ' sum' | [' sum' ] | {' x' : ' sum' } |
3297
+ + ------------ -+ ------------ + ------------ -+ -------------- -+
3298
+ | gb.apply(…) | x y z | | |
3299
+ | | z | | |
3300
+ | | 3 1 2 3 | | |
3301
+ | | 6 11 13 12 | | |
3302
+ + ------------ -+ ------------ + ------------ -+ -------------- -+
3303
+ | gb.agg(…) | x y | x y | x |
3304
+ | | z | sum sum | z |
3305
+ | | 3 1 2 | z | 3 1 |
3306
+ | | 6 11 13 | 3 1 2 | 6 11 |
3307
+ | | | 6 11 13 | |
3308
+ + ------------ -+ ------------ + ------------ -+ -------------- -+
3309
+ | gb.trans(…) | x y | | |
3310
+ | | a 1 2 | | |
3311
+ | | b 11 13 | | |
3312
+ | | c 11 13 | | |
3313
+ + ------------ -+ ------------ + ------------ -+ -------------- -+
3314
+ ```
3315
+
3316
+ #### Transformations:
3317
+ ``` python
3318
+ < DF > = < GB > .diff/ cumsum/ rank() # …/pct_change/fillna/ffill()
3261
3319
< DF > = < GB > .agg/ transform(< trans_func> )
3262
3320
```
3263
3321
3264
3322
``` python
3265
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + ------------ -+ -------------- -+
3266
- | | ' sum' | [' sum' ] | {' x' : ' sum' } | ' rank' | [' rank' ] | {' x' : ' rank' } |
3267
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + ------------ -+ -------------- -+
3268
- | gb.apply(…) | x y z | | | | | |
3269
- | | z | | | | | |
3270
- | | 3 1 2 3 | | | | | |
3271
- | | 6 11 13 12 | | | | | |
3272
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + ------------ -+ -------------- -+
3273
- | gb.agg(…) | x y | x y | x | x y | x y | x |
3274
- | | z | sum sum | z | a 1 1 | rank rank | a 1 |
3275
- | | 3 1 2 | z | 3 1 | b 1 1 | a 1 1 | b 1 |
3276
- | | 6 11 13 | 3 1 2 | 6 11 | c 2 2 | b 1 1 | c 2 |
3277
- | | | 6 11 13 | | | c 2 2 | |
3278
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + ------------ -+ -------------- -+
3279
- | gb.trans(…) | x y | | | x y | | |
3280
- | | a 1 2 | | | a 1 1 | | |
3281
- | | b 11 13 | | | b 1 1 | | |
3282
- | | c 11 13 | | | c 1 1 | | |
3283
- + ------------ -+ ------------ + ---------- -+ -------------- + -------- + ------------ -+ -------------- -+
3323
+ + ------------ -+ ------------ + ------------ -+ -------------- -+
3324
+ | | ' rank' | [' rank' ] | {' x' : ' rank' } |
3325
+ + ------------ -+ ------------ + ------------ -+ -------------- -+
3326
+ | gb.agg(…) | x y | x y | x |
3327
+ | | a 1 1 | rank rank | a 1 |
3328
+ | | b 1 1 | a 1 1 | b 1 |
3329
+ | | c 2 2 | b 1 1 | c 2 |
3330
+ | | | c 2 2 | |
3331
+ + ------------ -+ ------------ + ------------ -+ -------------- -+
3332
+ | gb.trans(…) | x y | | |
3333
+ | | a 1 1 | | |
3334
+ | | b 1 1 | | |
3335
+ | | c 1 1 | | |
3336
+ + ------------ -+ ------------ + ------------ -+ -------------- -+
3284
3337
```
3285
3338
3286
3339
### Rolling
3287
3340
``` python
3288
3341
< Rl_S/ D/ G> = < Sr/ DF / GB > .rolling(window_size) # Also: `min_periods=None, center=False`.
3289
3342
< Rl_S/ D> = < Rl_D/ G> [column_key/ s] # Or: <Rl>.column_key
3290
3343
< Sr/ DF / DF > = < Rl_S/ D/ G> .sum/ max / mean()
3291
- < Sr/ DF / DF > = < Rl_S/ D/ G> .apply(< func > ) # Invokes function on every window.
3344
+ < Sr/ DF / DF > = < Rl_S/ D/ G> .apply(< agg_func > ) # Invokes function on every window.
3292
3345
< Sr/ DF / DF > = < Rl_S/ D/ G> .aggregate(< func/ str > ) # Invokes function on every window.
3293
3346
```
3294
3347
0 commit comments