我有一個資料框如下:
| 第 1 列 | 列2 | 第 3 列 |
|---|---|---|
| 一種 | 乙 | C |
| 一種 | D | |
| D | 乙 | |
| 一種 | D | 乙 |
| 一種 | C |
并列出answer_key = ["A", "B", "C"]。
我想按順序將每列的值與串列的值進行比較。
根據以下規則回傳分數:無回應 = 0,成功回答 = 5,錯誤回答 = -5。另外,請回傳整個分數。
uj5u.com熱心網友回復:
這聽起來像是一個家庭作業問題,所以我只會為您提供偽代碼來幫助您指明正確的方向。我還假設您希望將每列中的內容與您的 answer_key 進行比較,并且不會動態添加這些內容。
# Create a list with your keys (you already did this)
# Create three separate lists for each column (col1, col2, col3)
# Also use something as a default value for values that are empty
# Ex1: col2 = ['E', None, 'B']
# Ex2: col2 = ['E', '0', 'B'] - either of these methods could work
# Create a dictionary to reference these list
cols = [0 : col1, 1 : col2, 2 : col3]
# Create an variable to store the entire score
score = 0
# Use nested loops to iterate through each column & each value
# example
for i in range(3):
# temporarily cache a list object for referrence
curList = cols.get(i)
# Compare contents of the key and list
for c in range(len(answer_key)):
# If curList[c] == None (or whatever value you
# are using for null) then score = 0
# If answer_key[c] == curList[c] then score = 5
# Else if answer_key[c] != curList[c] then score -= 5
uj5u.com熱心網友回復:
嘗試:
answer_key = np.array(["A", "B", "C"])
df['score'] = df.apply(lambda x: (pd.Series((x.to_numpy() == answer_key)).replace(False, -1).sum())*5 len(x[x.isnull()])*5, axis=1)
輸出:
col1 col2 col3 score
0 A E C 5
1 A NaN D 0
2 D B NaN 0
3 A D E -5
4 A NaN C 10
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/334221.html
