티스토리 뷰

Language/Python

pandas loc, apply적용 예제

KyeongRok Kim 2020. 3. 25. 16:30
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()
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
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
글 보관함