1

I am trying to loop over a dataframe. Especially through the date column so means for every date I get the x, y and z values for that date and fill it into my defined function. Somehow I am not sure how i can properly call it. My code looks like the following:

import pandas as pd

def calc_funct(x, y, z):

    func = x*y*z

    return func

if __name__ == '__main__':

    df = pd.read_csv('C:/Data.csv')

    for column in df:

        results = calc_funct(df['x'], df['y'], df['z'])
        print(result)

The input looks like the following:

           date   x   y   z
    0  2017-11-11  18  17   7
    1  2017-11-11  16  19   3
    2  2017-11-11  13  14   2
    3  2017-11-11  12  13   1
    4  2017-11-11  11  12   9
    5  2017-11-11  10  11  10
    6  2017-11-11  21  10  11
    7  2017-11-12  13  19  12
    8  2017-11-13  18  17  12
    9  2017-11-14   9  10  20
   10  2017-11-15   2  20  13
   11  2017-11-18  13  13   9
   12  2017-11-19  18  14  16
   13  2017-11-20  14  11  19
   14  2017-11-21  18  15  19

For date 2017-11-11 I would calculate the values (e.g. add/subtract all values them at that date) and store it e.g. in a list. Then iterate over the next date 2017-11-12 etc...

2
  • What is your expected output? Do you want a new column which gives the result of multiplying x, y and z for each row of your DataFrame? Commented Jan 30, 2018 at 10:53
  • @Ben, Thanks a lot for your answer. My Output should be just an print statement for every date it should print the result calculated based on the function defined. Commented Jan 30, 2018 at 11:00

2 Answers 2

4

In pandas you can use the apply method.

df.apply(lambda v : calc_funct(v["x"], v["y"], v["z"]), axis=1)

Note axis=1 for iterate over rows, axis=0 is for iteration over columns.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for the answer. How can I print the results as the following: Date and the result?
just add print : df.apply(lambda v : print(calc_funct(v["x"], v["y"], v["z"])), axis=1)
Note that you can add store the result in the original dataframe : df["result"] = df.apply(lambda v : calc_funct(v["x"], v["y"], v["z"]), axis=1)
2

If you want all the columns + the new column which is the result of your function, you can do so:

df['result'] = calc_funct(df['x'], df['y'], df['z'])

or just date and result with this other line of code:

df = df[['date','result']]

EDIT

result = []
for index, row in df.iterrows():
    result.append(row['date'])
    result.append(calc_funct(row['x'], row['y'], row['z']))
print result

14 Comments

And the ouput should be more stored outside of a dataframe.
what do you mean with "outside of a dataframe"?
just as an value in an array for later processing
I modified the post. In this way you have a list
Thanks a lot. This is the right way. But how can I include the date into the output and in addition why do I receive in output many time the same output?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.