2

python 新手問題

3 個回答

3
dragonH
iT邦大師 3 級 ‧ 2019-09-16 20:56:53

``````import pandas as pd
subject = ['國文', '英文', '數甲', '數乙', '物理', '化學', '生物', '歷史', '地理', '公民'
]
major = ['土木系', '財政系']

wg = [
[2, 2, 0, 1, 0, 0, 0, 1, 1, 0],
[1, 1.5, 0, 1.5, 0, 0, 0, 0, 0, 0],
]
score = [86.5, 79.4, 88.2, 96.5, 86.3, 76.8, 96.1, 78.5, 82.4, 88.6]
df2 = pd.DataFrame(wg, index = major, columns = subject)
df2['加權分數'] = df2.dot(score)
df2

``````
2
Austin
iT邦新手 5 級 ‧ 2019-09-16 21:54:30

python能不要用迴圈就盡量不要用

0
froce
iT邦大師 5 級 ‧ 2019-09-16 22:03:38

``````import pandas as pd

score = pd.Series([86.5, 79.4, 88.2, 96.5, 86.3, 76.8, 96.1, 78.5, 82.4, 88.6])

weight = pd.DataFrame(data=[
[2,2,0,1,0,0,0,1,1,0],
[1,1.5,0,1.5,0,0,0,0,0,0]
])

weight['加權分數'] = weight.dot(score)
print(weight)
``````

``````import pandas as pd

scores = pd.DataFrame([
[86.5, 79.4, 88.2, 96.5, 86.3, 76.8, 96.1, 78.5, 82.4, 88.6],
[87, 79, 90, 93.5, 96.3, 86.8, 95.1, 78.6, 86.4, 78.6]
])

weight = pd.DataFrame(data=[
[2,2,0,1,0,0,0,1,1,0],
[1,1.5,0,1.5,0,0,0,0,0,0]
])

# 矩鎮內積，所以有一方要轉置
print(weight.dot(scores.T))
``````