简体   繁体   中英

Loop over one column and fill rows in fucntion Pandas Dataframe Python

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 (eg add/subtract all values them at that date) and store it eg in a list. Then iterate over the next date 2017-11-12 etc...

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM