Pandas Excel: Add summation to a row of the given excel file
7. Add Summation to a Row
Write a Pandas program to add summation to a row of the given excel file. Go to Excel data
Sample Solution:
Python Code :
import pandas as pd
import numpy as np
df = pd.read_excel('E:\coalpublic2013.xlsx')
sum_row=df[["Production", "Labor_Hours"]].sum()
df_sum=pd.DataFrame(data=sum_row).T
df_sum=df_sum.reindex(columns=df.columns)
df_sum
Sample Output:
Year MSHA ID Mine_Name Production Labor_Hours 0 NaN NaN NaN 27854323 11098816
Excel Data:
coalpublic2013.xlsx:
For more Practice: Solve these Related Problems:
- Write a Pandas program to import coalpublic2013.xlsx and add a new column that contains the row-wise summation of numeric fields.
- Write a Pandas program to calculate the sum of all numeric columns in each row of the Excel data and append it as a new column.
- Write a Pandas program to add a totals row to a DataFrame derived from coalpublic2013.xlsx by summing each column.
- Write a Pandas program to compute a new column that is the sum of selected columns from the imported Excel file.
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 ) skipping first twenty rows into a Pandas dataframe.
Next: Write a Pandas program to import given excel data (coalpublic2013.xlsx ) into a Pandas dataframe and display the last ten rows.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.