我有一个数据框,如下所示:-
,issue_name,doc_id,doc_type,doc_title
0,The App keeps crashing / restarting / hanging,5b519e219b989aaf3db06917,GUIDE,Restart the device
1,The App keeps crashing / restarting / hanging,5b519e219b989aaf3db06917,GUIDE,Restart the device
2,The App keeps crashing / restarting / hanging,5b51a24d9b989aaf3db0691a,GUIDE,Fix the App
3,The App keeps crashing / restarting / hanging,5b51a24d9b989aaf3db0691a,GUIDE,Fix the App
4,The App keeps crashing / restarting / hanging,5b519e219b989aaf3db06917,GUIDE,Restart the device
5,The App keeps crashing / restarting / hanging,5b519e219b989aaf3db06917,GUIDE,Restart the device
当我使用下面的代码汇总计数时:-
dfreturns = pd.DataFrame(Guidedocdetails, columns=['issue_name','doc_id','doc_type','doc_title'])
dfreturns.to_csv('ReturnGuideDocDetails.csv')
dfreturnguidecount = dfreturns.groupby(['issue_name','doc_type','doc_title']).agg(['count'])
dfreturnguidecount.to_csv('Return_guideid_counts.csv')
请帮助我了解如何实现这一目标。
应用以下代码后:-
dfnonreturnguidecount = (dfnonreturns.groupby(['issue_name','doc_type','doc_title'])['issue_name'].count().reset_index(name='count'))
dfnonreturnguidecount.to_csv('NonReturn_guideid_counts.csv')
答案 0 :(得分:1)
我认为需要使用GroupBy.size
或GroupBy.count
在列中删除MultiIndex
:
returnguidecount = (dfreturns.groupby(['issue_name','doc_type','doc_title'])
.size()
.reset_index(name='count'))
returnguidecount = (dfreturns.groupby(['issue_name','doc_type','doc_title'])['issue_name']
.count()
.reset_index(name='count'))
print (returnguidecount)
issue_name doc_type doc_title \
0 The App keeps crashing / restarting / hanging GUIDE Fix the App
1 The App keeps crashing / restarting / hanging GUIDE Restart the device
count
0 2
1 4
差异是count
排除groupby
之后指定列中的NaNs值。