Calculate Sum by Group in Python (2 Examples) | DataFrame Subgroups | groupby() & sum() Functions

preview_player
Показать описание
Python code of this video:

import pandas as pd # Import pandas

data = pd.DataFrame({'x1':[6, 5, 2, 2, 6, 7, 8], # Create pandas DataFrame
'x2':range(22, 29),
'group1':['C', 'B', 'B', 'A', 'C', 'B', 'A'],
'group2':['a', 'a', 'a', 'a', 'b', 'b', 'b']})
print(data) # Print pandas DataFrame

# x1 x2
# group1
# A 10 53
# B 14 74
# C 12 48

# x1 x2
# group1 group2
# A a 2 25
# b 8 28
# B a 7 47
# b 7 27
# C a 6 22
# b 6 26

Follow me on Social Media:

Рекомендации по теме