Soubhik Banerjee
Soubhik Banerjee

Reputation: 441

Formatting headers for aggregated dataframe in Python

I have a dataframe as below:-

,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

when i am aggregating the same for count with below code:-

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') 

i get the output as below:enter image description here

How do i remove the doc_id and extra lines coming in top. I want output as below: enter image description here

Kindly help me understand how can i achieve the same.

After applying below code:-

dfnonreturnguidecount = (dfnonreturns.groupby(['issue_name','doc_type','doc_title'])['issue_name'].count().reset_index(name='count'))
dfnonreturnguidecount.to_csv('NonReturn_guideid_counts.csv')

output:- enter image description here

Upvotes: 1

Views: 32

Answers (1)

jezrael
jezrael

Reputation: 862791

I think need for remove MultiIndex in columns use GroupBy.size or GroupBy.count:

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  

Difference is count exclude NaNs values in column specify after groupby.

Upvotes: 1

Related Questions