Shantanu Shantanu - 1 year ago 246
Python Question

Python Dataframe for loop syntax

I'm having trouble in correctly executing a for loop through my dataframe in python.

Basically, for every row in the dataframe (df_weather), the code should select one value each from the column no. 13 and 14 and execute a function which is defined earlier in the code. Eventually, I require the calculated value in each row to be summed to give me one final answer.

The error being returned is as follows: "string indices must be integers"

Request anyone to help me through this step. The code for the same is provided below.


stress_rate = 0
for i in df_weather:
b = GetStressDampHeatParameterized(i[:,13], i[:,14])
stress_rate = b + stress_rate

Answer Source

This can be solved in a single line:

print sum(df.apply(lambda row: func(row[14], row[15]), axis=1))

Where func is your desired function and axis=1 ensures that the function is applied on each row as opposed to each column (which is the default).

My solution first creates a temporary series (picture: an unattached column) that is constructed by applying a function to each row in turn. The function that is actually being applied is an anonymous function indicated by the keyword lambda, which takes a single input row and which is fed a single row at a time from the apply method. That anonymous function simply calls your function func and passes the two column values in the row.

A Series can be summed using the sum function.

Note the indexing of the columns starts at 0.

Also note, saying for x in df: will iterate over the columns.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download