how to sum each row and replace each value in row with sum?

  • Last Update :
  • Techknowledgy :

Use mask for replace all non missing values by sum:

df = df.mask(df.notnull(), df.sum(axis = 1), axis = 0)
print(df)
1 2 3 4 5
1 14 14 14 NaN 14
2 17 17 17 17.0 17
3 7 7 7 7.0 7
arr = df.values
a = np.broadcast_to(np.nansum(arr, axis = 1)[: , None], df.shape)
df = pd.DataFrame(np.where(np.isnan(arr), np.nan, a),
   index = df.index, columns = df.columns)

#alternative
df[: ] = np.where(np.isnan(arr), np.nan, a)
print(df)
1 2 3 4 5
1 14.0 14.0 14.0 NaN 14.0
2 17.0 17.0 17.0 17.0 17.0
3 7.0 7.0 7.0 7.0 7.0

Using mul

df.notnull().replace(False, np.nan).mul(df.sum(1), axis = 0).astype(float)
1 2 3 4 5
1 14.0 14.0 14.0 NaN 14.0
2 17.0 17.0 17.0 17.0 17.0
3 7.0 7.0 7.0 7.0 7.0

Suggestion : 2

Program to find the Sum of each Row and each Column of a Matrix,Given a matrix of order m×n, the task is to find out the sum of each row and each column of a matrix.,The sum of each row and each column can be calculated by traversing through the matrix and adding up the elements.,Check if sums of i-th row and i-th column are same in matrix

Examples: 

Input: array[4][4] = {
   {
      1,
      1,
      1,
      1
   },
   {
      2,
      2,
      2,
      2
   },
   {
      3,
      3,
      3,
      3
   },
   {
      4,
      4,
      4,
      4
   }
};
Output: Sum of the 0 row is = 4
Sum of the 1 row is = 8
Sum of the 2 row is = 12
Sum of the 3 row is = 16
Sum of the 0 column is = 10
Sum of the 1 column is = 10
Sum of the 2 column is = 10
Sum of the 3 column is = 10

Finding Sum of each row:

   Sum of the row 0 = 10
Sum of the row 1 = 26
Sum of the row 2 = 42
Sum of the row 3 = 58

Finding Sum of each column:

   Sum of the column 0 = 28
Sum of the column 1 = 32
Sum of the column 2 = 36
Sum of the column 3 = 40

Finding Sum of each row:

   Sum of the row 0 = 10
Sum of the row 1 = 26
Sum of the row 2 = 42
Sum of the row 3 = 58

Finding Sum of each column:

   Sum of the column 0 = 28
Sum of the column 1 = 32
Sum of the column 2 = 36
Sum of the column 3 = 40

Suggestion : 3

Use mask for replace all non missing values by sum: ,How to check if data is missing after two or more repeating values in pandas and replace missing value with previous value?,How to replace value in specific index in each row with corresponding value in numpy array,how to calculate percentage for each cell and replace tha value with result(%) using pandas dataframe?

Use mask for replace all non missing values by sum:

df = df.mask(df.notnull(), df.sum(axis = 1), axis = 0)
print(df)
1 2 3 4 5
1 14 14 14 NaN 14
2 17 17 17 17.0 17
3 7 7 7 7.0 7
arr = df.values
a = np.broadcast_to(np.nansum(arr, axis = 1)[: , None], df.shape)
df = pd.DataFrame(np.where(np.isnan(arr), np.nan, a),
   index = df.index, columns = df.columns)

#alternative
df[: ] = np.where(np.isnan(arr), np.nan, a)
print(df)
1 2 3 4 5
1 14.0 14.0 14.0 NaN 14.0
2 17.0 17.0 17.0 17.0 17.0
3 7.0 7.0 7.0 7.0 7.0

Using mul

df.notnull().replace(False, np.nan).mul(df.sum(1), axis = 0).astype(float)
1 2 3 4 5
1 14.0 14.0 14.0 NaN 14.0
2 17.0 17.0 17.0 17.0 17.0
3 7.0 7.0 7.0 7.0 7.0

Suggestion : 4

If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series.,Exclude NA/null values when computing the result.,Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.,Thanks to the skipna parameter, min_count handles all-NA and empty series identically.

>>> idx = pd.MultiIndex.from_arrays([
         ...['warm', 'warm', 'cold', 'cold'],
         ...['dog', 'falcon', 'fish', 'spider']
      ],
      ...names = ['blooded', 'animal']) >>>
   s = pd.Series([4, 2, 0, 8], name = 'legs', index = idx) >>>
   s
blooded animal
warm dog 4
falcon 2
cold fish 0
spider 8
Name: legs, dtype: int64
>>> s.sum()
14
>>> pd.Series([], dtype = "float64").sum() # min_count = 0 is the
default
0.0
>>> pd.Series([], dtype = "float64").sum(min_count = 1)
nan
>>> pd.Series([np.nan]).sum()
0.0
>>> pd.Series([np.nan]).sum(min_count = 1)
nan

Suggestion : 5

The following syntax illustrates how to compute the rowSums of each row of our data frame using the replace, is.na, mutate, and rowSums functions.,Have a look at the previous output: We have created a data frame with an additional column showing the sum of each row. Note that the NA values were replaced by 0 in this output.,In this Example, I’ll explain how to use the replace, is.na, summarise_all, and sum functions.,Have a look at the previous output of the RStudio console. It shows that our exemplifying data contains five rows and four columns. Note that all of the variables are numeric and some of the variables contain NA values (i.e. missing values).

data < -data.frame(x1 = 1: 5, # Example data x2 = c(NA, 5, 1, 1, NA),
   x3 = 9: 5,
   x4 = c(4, 1, NA, 2, 8))
data # Print example data
# x1 x2 x3 x4
# 1 1 NA 9 4
# 2 2 5 8 1
# 3 3 1 7 NA
# 4 4 1 6 2
# 5 5 NA 5 8
install.packages("dplyr") # Install & load dplyr
library("dplyr")
data % > % # Compute column sums
replace(is.na(.), 0) % > %
   summarise_all(sum)
# x1 x2 x3 x4
# 1 15 7 35 15
data % > % # Compute row sums
replace(is.na(.), 0) % > %
   mutate(sum = rowSums(.))
# x1 x2 x3 x4 sum
# 1 1 0 9 4 14
# 2 2 5 8 1 16
# 3 3 1 7 0 11
# 4 4 1 6 2 13
# 5 5 0 5 8 18