CharlesAlakis
CharlesAlakis

Reputation: 45

Sum column values for each row

I would sum each row of this dataframe based on a column.

Input Dataframe:

    a   b
0   red 5
1   red 8
2   red 2
3   red 4
4   red 1

I used this code to sum values sumcol = df['b'].sum() but this sum the entire column I would a value for each row -1 in a new column.

Expected Dataframe:

    a   b   c
0   red 5   15
1   red 8   7
2   red 2   5
3   red 4   1
4   red 1   0

Upvotes: 3

Views: 998

Answers (1)

Hamza usman ghani
Hamza usman ghani

Reputation: 2243

Use apply lambda with axis=1.
x.name will give you the current index for each row. Simple make slices for each row using x.name+1: which means get all below rows.

df["c"] = df.apply(lambda x: df.loc[x.name+1:,"b"].sum(),axis=1)

print(df)

         a  b   c
    0   red 5   15
    1   red 8   7
    2   red 2   5
    3   red 4   1
    4   red 1   0

And if you wants to get summation/number_of_values which seems like a case of mean() then try:

df["c"] = df.apply(lambda x: df.loc[x.name+1:,"b"].mean(),axis=1)
print(df)

         a  b    c
    0   red 5   3.750000
    1   red 8   2.333333
    2   red 2   2.500000
    3   red 4   1.000000
    4   red 1   NaN

Upvotes: 1

Related Questions