ali
ali

Reputation: 71

Loop on pandas DataFrame

I would like to run a loop over rows of pandas DataFrame such that based on indices in columns a and b I can sum the values given in column f and can tag them in a separate column by a string name.

    a   b   c   d   e   f
0   1.0 2.0 0   0   0   2.567483
1   1.0 3.0 -1  0   0   2.567483
2   3.0 1.0 1   0   0   2.567483
3   1.0 2.0 -1  -1  0   2.567483

Presently I am using if conditions of DataFrame, but this makes my code less elegant. Thanks for your suggestions in advance!

Upvotes: 1

Views: 3976

Answers (1)

David Meu
David Meu

Reputation: 1545

You can use iterrows():

import pandas as pd

df = pd.DataFrame({'a': [10, 11, 12], 'b': [100, 11, 120], 'f': [100, 110, 120]})

for index, row in df.iterrows():
    if row['a'] == row['b']:
        print(row['f'])

Outputs:

110

Or you can use groupby:

import pandas as pd

df = pd.DataFrame({'a': [1.0, 1.0, 3.0, 1.0], 'b': [2.0, 3.0, 1.0, 2.0],
                   'f': [2.567483, 2.567483, 2.567483, 2.567483]})

group_by_a_b = df.groupby(["a", "b"]).f.sum().reset_index()
print(group_by_a_b)

Outputs:

     a    b         f
0  1.0  2.0  5.134966
1  1.0  3.0  2.567483
2  3.0  1.0  2.567483

Upvotes: 2

Related Questions