Count Rows by Group in pandas DataFrame in Python (Examples) | Number of Cases | groupby() & size()

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

import pandas as pd # Import pandas library

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

# group1
# A 7
# B 2
# C 4
# dtype: int64

# group1 group2
# A a 5
# b 2
# B a 1
# b 1
# C a 2
# b 2
# dtype: int64

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

thanks for sharing. how to give a name to the new column?

aisyahnur