2016-04-17 66 views
3

我有一個2個數據框 用於打分學生分數的單一分數卡 Second One-Student數據集。引用列表中的列表

我想在給定的學生數據集上應用記分卡來計算分數並聚合它們。我試圖devlop一個通用的函數,它的 記分卡,適用於任何studentmarks數據集

import pandas as pd 
score_card_data = { 
     'subject_id': ['MATHS', 'SCIENCE', 'ARTS'], 
     'bin_list': [[0,25,50,75,100], [0,20,40,60,80,100], [0,20,40,60,80,100]], 
     'bin_value': [[1,2,3,4], [1,2,3,4,5], [3,4,5,6,7] ]} 
score_card_data = pd.DataFrame(score_card_data, columns = ['subject_id', 'bin_list', 'bin_value']) 
score_card_data 

student_scores = { 
     'STUDENT_ID': ['S1', 'S2', 'S3','S4','S5'], 
     'MATH_MARKS': [10,15,25,65,75], 
     'SCIENCE_MARKS': [8,15,20,35,85], 
     'ARTS_MARKS':[55,90,95,88,99]} 
student_scores = pd.DataFrame(student_scores, columns = ['STUDENT_ID', 'MATH_MARKS', 'SCIENCE_MARKS','ARTS_MARKS']) 
student_scores 

功能 定義箱 應用垃圾箱超過列

bins = list(score_card_data.loc[score_card_data['subject_id'] == 'MATHS', 'bin_list']) 
student_scores['MATH_SCORE'] = pd.cut(student_scores['MATH_MARKS'],bins, labels='MATHS_MARKS') 

Error:ValueError: object too deep for desired array 

我想將cellvalue轉換爲字符串,並將其作爲對象進行檢測。任何方式來解決

如何使該功能更通用?

感謝 帕裏

回答

0

你可以使用bins[0]提取list,否則引發ValueError

bins[0] 
[0, 25, 50, 75, 100] 

type(bins[0]) 
<class 'list'> 

student_scores['MATH_SCORE'] = pd.cut(student_scores['MATH_MARKS'], bins[0]) 

    STUDENT_ID MATH_MARKS SCIENCE_MARKS ARTS_MARKS MATH_SCORE 
0   S1   10    8   55 (0, 25] 
1   S2   15    15   90 (0, 25] 
2   S3   25    20   95 (0, 25] 
3   S4   65    35   88 (50, 75] 
4   S5   75    85   99 (50, 75] 

我離開了labels,因爲你需要提供的四個列表給定的標籤有五個截斷/邊緣。