![](/img/trans.png)
[英]A clean and efficient way to update cells in pandas DataFrames
[英]Efficient and Clean way for writting this code - Pandas
我一直在研究此代碼,該代碼對我的數據框的3個作業和1個測試的得分進行匯總,並根據得分生成標記(A,B,C,D,F)。
這是我的數據框。
StudentId, Assignment1, Assignment2, Assignment3, Test
xxxxxxxx 11 15 7 50
yyyyyyyy 5 10 2 31
這是我總結分數並生成分數的代碼
SumScoreX = []
GradeX = []
for x in xrange(len(df)):
A1, A2, A3, T1 = df['Assignment1'][x], df['Assignment2'][x],df['Assignment3'][x], df['Test'][x]
SumScore = np.sum([A1,A2,A3,T1])
if SumScore < 51:
Grade = 'F'
elif SumScore == 50 and SumScore < 60:
Grade = 'D'
elif SumScore == 60 and SumScore < 70:
Grade = 'C'
elif SumScore == 70 and SumScore < 80:
Grade = 'B'
elif SumScore <= 80:
Grade = 'A'
SumScoreX.append(np.round(SumScore))
GradeX.append(Grade)
我的代碼看起來很臟。 我覺得應該有一個更好的方法來編寫具有與此代碼相似功能的代碼。
請建議我。
謝謝!
假設您具有以下DF:
In [100]: df
Out[100]:
StudentId Assignment1 Assignment2 Assignment3 Test
0 xxxxxxxx 11 15 7 50
1 yyyyyyyy 5 10 2 31
首先計算score
:
In [101]: df['score'] = df.filter(regex=r'(?:Assignment\d*|Test)').sum(1)
現在我們可以使用pd.cut()方法對得分進行分類:
In [102]: df['grade'] = pd.cut(df.score, bins=[0, 51, 60, 70, 80, 200], labels=list('FDCBA'))
In [103]: df
Out[103]:
StudentId Assignment1 Assignment2 Assignment3 Test score grade
0 xxxxxxxx 11 15 7 50 83 A
1 yyyyyyyy 5 10 2 31 48 F
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.