Python Forum

Full Version: creating a loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have a line of code that works fine for a single column in the data frame. I would now like to convert this so that it finds all the columns in the data frame (returnsall)and passes them through this line.

The line of code is
stock= 'T.TO'
seriesObj = returnsall.apply(lambda x: True if x[stock] > 2 else False , axis=1)
# Count number of True in series
numOfRows = len(seriesObj[seriesObj == True].index)
print('Number of Rows in dataframe in which change > 5 : ', numOfRows)
The returnsall dataframe looks like this:

BCE.TO RCI-B.TO T.TO
Date
2010-01-05 -1.476648 -1.827309 -0.087740
2010-01-06 -0.522831 -0.192583 0.087817
2010-01-07 -2.592855 -0.482278 -0.380206
2010-01-08 0.143890 1.357011 0.763314
2010-01-11 -0.395117 1.880815 -0.641015

Any help would be appreciated. thanks.
Any thoughts anyone
Could you share a larger example that we can run and play around with?