創建另一列,您可以使用groupby來計算:
df = pd.DataFrame({'is_correct':['t','t','f','f','t','t'],'question_id':[1,1,1,1,2,2]})
df['to_sum_up']=1
is_correct question_id to_sum_up
t 1 1
t 1 1
f 1 1
f 1 1
t 2 1
t 2 1
df2 = df.groupby(['question_id','is_correct'],as_index = False).sum()
一旦你做出GROUPBY,你只需要重新排列數據,以便它適合的列你想:
df2['correct_count'] = df2.ix[df2['is_correct']=='t','N']
df2['incorrect_count'] = df2.ix[df2['is_correct']=='f','N']
然後纔能有一個很好的數據幀作爲輸出:
df2.ix[df2['correct_count'].isnull(),'correct_count'] = 0
df2.ix[df2['incorrect_count'].isnull(),'incorrect_count'] = 0
df2 = df2.groupby('question_id',as_index = False).max()
df2 = df2.drop(['N','is_correct'],1)
question_id correct_count incorrect_count
0 1 2 2
1 2 2 0
[python pandas可能的重複:如何分組和按列中的每個值的條件?](http://stackoverflow.com/questions/31458703/python-pandas-how-to-組按和計數上帶有一個條件換每值-i的n-a-c) –
它是重複的。雖然MaxU對這個問題的解決方案比另一個有更好更有趣的答案 – samol
然後,請好好將另一個問題標記爲這個問題的重複,這樣所有問題都會被引導到這個問題上。 –