0

I need to calculate the sum of specific rows in my dataframe

for example, Nombre de reboot, Passage en mode privé, Passage en mode public, Nombre de Kilomètres parcourus, Heures de roulage,Temps de trajet..

I tested this code on the first three rows:

import pandas as pd
df = pd.read_excel('mycollected_data1.xlsx')


print (df.iloc[:3, df.columns.get_indexer(['Valeurs','Valeurs.1','Valeurs.2'])])
a = df.iloc[1:3, [1,2,3]].sum()

print ('a\n', a)

this is the output:

    Valeurs Valeurs.1 Valeurs.2
0       3         5         0
1       2         1         1
2       0         0         0
a
 Valeurs      2.0
Valeurs.1    1.0
Valeurs.2    1.0
dtype: float64

desired output:

        Valeurs Valeurs.1 Valeurs.2   sum
    0       3         5         0       8
    1       2         1         1       4
    2       0         0         0       0

how can I make it calculate the sum of specific given rows?

2 Answers 2

2

You need to use axis=1 in your sum function

Original df

df
   Valeurs  Valeurs.1  Valeurs.2
0        3          5          0
1        2          1          1
2        0          0          0


cols = ['Valeurs','Valeurs.1','Valeurs.2']

df['sum'] = df.loc[0:2, cols].sum(axis=1)
df # final df

   Valeurs  Valeurs.1  Valeurs.2  sum
0        3          5          0    8
1        2          1          1    4
2        0          0          0    0
Sign up to request clarification or add additional context in comments.

Comments

2

IIUC,

we can use .sum while specifying the axis as 1 to work row wise.

cols = ['Valeurs','Valeurs.1','Valeurs.2']

df['sum'] = df[cols].sum(axis=1)

print(df)

   Valeurs  Valeurs.1  Valeurs.2  sum
0        3          5          0    8
1        2          1          1    4
2        0          0          0    0

edit, if you need to access certain rows you can use the .loc function as @quant has

row_start = 0
row_end = 2
df.loc[row_start:row_end,cols].sum(axis=1)

Just as a word of advice, it seems like you have repeated column names with similar datatypes, i would first clean your column headings and then melt your dataframe to get a tabular model.

2 Comments

thank you, but how can I specify the row that I need to work on ?
as in you only want to sum a certain index location? will the other values just be NaN?if so you can use loc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.