본문 바로가기

Language/Python

pandas loc, apply적용 예제

data = {'contestants': ('Alex', 'John', 'Sam', 'Alex', 'John', 'Sam', 'Alex', 'John', 'Sam'),
     'round': (1, 1, 1, 2, 2, 2, 3, 3, 3),
     'score1': (84.5, 91.4, 92.2, 85.2, 88.1, 83.1, 80.2, 93.5, 82.1),
     'score2': (88.2, 89.8, 88.7, 86.3, 89.0, 83.1, 79.9, 92.8, 80.9),
     'score3': (84.1, 90.2, 93.2, 89.0, 90.1, 83.0, 81.0, 92.2, 82.3),
     'score4': (85.9, 92.0, 90.2, 88.1, 87.0, 85.0, 83.2, 93.0, 86.3),
     'score5': (84.7, 90.0, 88.1, 82.9, 89.2, 84.1, 82.7, 90.8, 86.0),
     'difficulty': (3.5, 3.2, 3.2, 3.6, 3.5, 3.6, 3.8, 3.2, 3.7)}

df = pd.DataFrame(data)

0번 행에 score1선택하기

print(df.loc[0]['score1'])
print(df.loc[0, 'score1'])

0번행에 index 2번부터 7번까지 선택하기

print(df.loc[0][2:7])

score1 ~ score5 sum구해서 middle_sum에 넣기

df['middle_sum'] = df.apply(lambda x: x[2:7].sum() , axis=1)

group by

df_group = df[['contestants', 'middle_sum']].groupby(['contestants']).sum()