w3resource

Pandas Excel: Create a subtotal of "Labor Hours" against MSHA ID from the given excel data

Pandas: Excel Exercise-9 with Solution

Write a Pandas program to create a subtotal of "Labor Hours" against MSHA ID from the given excel data (coalpublic2013.xls ). Go to Excel data

Sample Solution:

Python Code :

import pandas as pd
import numpy as np
df = pd.read_excel('E:\coalpublic2013.xlsx')
df_sub=df[["MSHA ID","Labor_Hours"]].groupby('MSHA ID').sum()
df_sub

Sample Output:

         Labor_Hours
MSHA ID             
100329        144002
100347        215295
100759        474784
100851       1001809
101247       1551141
101362        116914
101401       2464719
102901       1249811
102976         38021
102996        164093
103155         79990
103172        119542
103179         63745
103180        196963
103182         87314
103195         17411
103246         29193
103285         90584
103303        164388
103321         76366
103323         46381
103332         61394
103342        115123
103358        161805
103364         14324
103370           621
103372         32401
103375          1900
103376        176499
103380         14023
103381         22392
103389         84966
103404         28447
103410        158591
103419        107469
103422        140250
103423          9162
103432           220
103433         47195
103436         77190
103437         70926
103451         46393
103467         30539
201195       1015333
1519322         3108
5000030       286079	                                       

Excel Data:

coalpublic2013.xlsx:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to import given excel data (coalpublic2013.xlsx ) into a Pandas dataframe and display the last ten rows.
Next: Write a Pandas program to import given excel data (coalpublic2013.xls ) into a dataframe and find a specific MSHA ID.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Split a String:

>>> sentence = 'this is, a python, tutorial, about, idioms.'
>>> sentence.split(', ')
['this is', 'a python', 'tutorial', 'about', 'idioms.']
>>> sentence.split(', ', 2)
['this is', 'a python', 'tutorial, about, idioms.']
>>> sentence.rsplit(', ')
['this is', 'a python', 'tutorial', 'about', 'idioms.']
>>> sentence.rsplit(', ', 2)
['this is, a python, tutorial', 'about', 'idioms.']