Skip to content Skip to sidebar Skip to footer

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. Som

Solution 1:

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.


Solution 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

Post a Comment for "Loop Over One Column And Fill Rows In Fucntion Pandas Dataframe Python"