Skip to content

Commit 415e537

Browse files
committed
Pandas
1 parent db9ba04 commit 415e537

File tree

2 files changed

+183
-89
lines changed

2 files changed

+183
-89
lines changed

README.md

Lines changed: 96 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,7 +3076,7 @@ from pandas import Series, DataFrame
30763076
**Ordered dictionary with a name.**
30773077

30783078
```python
3079-
>>> Series([1, 2], index=['x', 'y'], name='a')
3079+
>>> sr = Series([1, 2], index=['x', 'y'], name='a')
30803080
x 1
30813081
y 2
30823082
Name: a, dtype: int64
@@ -3110,22 +3110,37 @@ Name: a, dtype: int64
31103110
<Sr> = <Sr>.combine_first(<Sr>) # Adds items that are not yet present (extends).
31113111
```
31123112

3113-
#### Operations:
3113+
#### Aggregations:
31143114
```python
31153115
<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
31163132
<Sr> = <Sr>.diff/cumsum/rank/pct_change() # …/fillna/ffill/interpolate()
3117-
<el> = <Sr>.apply/agg(<agg_func>)
31183133
<Sr> = <Sr>.apply/agg/transform(<trans_func>)
31193134
```
31203135

31213136
```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+
+-------------+--------+-----------+---------------+
31293144
```
31303145

31313146
### DataFrame
@@ -3168,27 +3183,44 @@ b 3 4
31683183
<DF> = <DF>.melt(id_vars=column_key/s) # Melts on columns.
31693184
```
31703185

3171-
#### Operations:
31723186
```python
31733187
<Sr> = <DF>.sum/max/mean/idxmax/all()
3174-
<DF> = <DF>.diff/cumsum/rank() # …/pct_change/fillna/ffill/interpolate()
31753188
<Sr> = <DF>.apply/agg/transform(<agg_func>)
3189+
<DF> = <DF>.diff/cumsum/rank() # …/pct_change/fillna/ffill/interpolate()
31763190
<DF> = <DF>.apply/agg/transform(<trans_func>)
31773191
<DF> = <DF>.applymap(<func>) # Apply a function to a Dataframe elementwise.
31783192
```
31793193
* **All operations operate on columns by default. Use `'axis=1'` parameter to process the rows instead.**
31803194

3195+
#### Apply, Aggregate, Transform:
31813196
```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
31903201
```
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'}`.**
31923224

31933225
#### Merge, Join, Concat:
31943226
```python
@@ -3253,42 +3285,63 @@ c 6 7
32533285
```
32543286
* **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.**
32553287

3256-
#### Operations:
3288+
#### Aggregations:
32573289
```python
32583290
<DF> = <GB>.sum/max/mean/idxmax/all()
3259-
<DF> = <GB>.diff/cumsum/rank() # …/pct_change/fillna/ffill()
32603291
<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()
32613319
<DF> = <GB>.agg/transform(<trans_func>)
32623320
```
32633321

32643322
```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+
+-------------+------------+-------------+---------------+
32843337
```
32853338

32863339
### Rolling
32873340
```python
32883341
<Rl_S/D/G> = <Sr/DF/GB>.rolling(window_size) # Also: `min_periods=None, center=False`.
32893342
<Rl_S/D> = <Rl_D/G>[column_key/s] # Or: <Rl>.column_key
32903343
<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.
32923345
<Sr/DF/DF> = <Rl_S/D/G>.aggregate(<func/str>) # Invokes function on every window.
32933346
```
32943347

0 commit comments

Comments
 (0)