728x90
반응형
In [16]:
import pandas as pd
In [17]:
raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons', 'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],
'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'],
'name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze', 'Jacon', 'Ryaner', 'Sone', 'Sloan', 'Piger', 'Riani', 'Ali'],
'preTestScore': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
'postTestScore': [25, 94, 57, 62, 70, 25, 94, 57, 62, 70, 62, 70]}
In [18]:
regiment = pd.DataFrame(raw_data,columns=raw_data.keys())
regiment.head()
Out[18]:
In [19]:
regiment[regiment['regiment'] == 'Nighthawks'].groupby('regiment').mean()
Out[19]:
In [20]:
regiment.groupby('company').describe()
Out[20]:
In [21]:
regiment.groupby('company')['preTestScore'].mean()
Out[21]:
In [24]:
regiment.groupby(['regiment','company']).preTestScore.mean()
Out[24]:
In [26]:
regiment.groupby(['regiment','company']).preTestScore.mean().unstack()
Out[26]:
In [28]:
regiment.groupby(['regiment','company']).mean()
Out[28]:
In [32]:
# unstack으로 보기 좋게
regiment.groupby(['regiment','company']).size().unstack()
Out[32]:
In [41]:
for name, reg in regiment.groupby('regiment'):
print(name)
print(reg)
728x90
반응형