Pandas Excel: Create a subtotal of "Labor Hours" against MSHA ID from the given excel data
9. Subtotal of Labor Hours by MSHA ID
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:
For more Practice: Solve these Related Problems:
- Write a Pandas program to group coalpublic2013.xlsx data by MSHA ID and calculate the total Labor Hours for each group.
- Write a Pandas program to import Excel data and create a pivot table that sums "Labor Hours" by MSHA ID.
- Write a Pandas program to compute a subtotal of Labor Hours for each unique MSHA ID in the DataFrame.
- Write a Pandas program to aggregate the "Labor Hours" field grouped by MSHA ID and then display the results in descending order.
Go to:
PREV : Display Last Ten Rows of Excel Data.
NEXT : Find Specific MSHA ID.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
