forked from lshang0311/pandas-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupby_split_apply_combine.py
53 lines (39 loc) · 1.07 KB
/
groupby_split_apply_combine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import numpy as np
import pandas as pd
"""
https://jakevdp.github.io/PythonDataScienceHandbook/03.08-aggregation-and-grouping.html
"""
rng = np.random.RandomState(0)
df = pd.DataFrame(
{
'key': ['A', 'B', 'C', 'A', 'B', 'C'],
'data1': range(6),
'data2': rng.randint(0, 10, 6)
},
columns=['key', 'data1', 'data2']
)
print(df)
def f_norm(x):
print("group:")
print(x)
return x / x.sum()
def f_sum(x):
return x.sum()
def norm_by_data2(x):
# x is a DataFrame of group values
x['data1'] /= x['data2'].sum()
return x
# transformation
print(df.groupby('key').transform(lambda x: x - x.mean()))
print(df.groupby('key').transform(f_norm))
df[['data1_group_sum', 'data2_group_sum']] = df.groupby('key').transform(f_sum)
print(df)
# apply()
print(df.groupby('key').apply(norm_by_data2))
# specifying the split key
L = [0, 0, 1, 1, 1, 1]
print(df.groupby(L).sum())
# a dictionary mapping index to group
df2 = df.set_index(['key'])
mapping = {'A': 'vowel', 'B': 'consonant', 'C': 'consonant'}
print(df2.groupby(mapping).sum())