I'm writing a function part of which should iterate through the rows of a Series
. The function should iterate through rows of a DataFrame
column passed to it i.e. df['col'], however when I try to use .iterrows
I get an error that a Series
doesn't have that attribute and using .iteritems
produces the error below. Are there any other ways to iterate through rows of a column? I need do be able to access the index and column value.
def get_RIMsin(df, obs, rimcol):
"""dataframe, obs column,rim column"""
maxval =df['Mmedian'].max()
minval =df['Mmedian'].min()
dfrange = maxval-minval
amplitude = dfrange/2
convert = (2*np.pi)/365
startday = obs.idxmax().dayofyear
sinmax = 91
for row in rimcol.iteritems: #This is where I'd like to go through rows of a series
diff = sinmax - startday
adjday = row.dayofyear + diff
adjsin = np.sin(adjday * convert)
df['RIMsine'] = row + adjsin
return df
get_RIMsin(sve_DOC, sve_DOC['DOC_mg/L'], sve_DOC['RIMsDOC'])
TypeError Traceback (most recent call last)
<ipython-input-98-4811cbf80e78> in <module>()
17 return df
18
---> 19 get_RIMsin(sve_DOC, sve_DOC['DOC_mg/L'], sve_DOC['RIMsDOC'])
20 """get_RIM2(svv_DOC, svv_DOC['DOC_mg/L'], svv_DOC['RIMsDOC'])
21 get_RIM2(svw_DOC, svw_DOC['DOC_filt_mg/l'], svw_DOC['RIMsDOC'])
<ipython-input-98-4811cbf80e78> in get_RIMsin(df, obs, rimcol)
10 sinmax = 91
11
---> 12 for row in rimcol.iteritems:
13 diff = sinmax - startday
14 adjday = row.dayofyear + diff
TypeError: 'instancemethod' object is not iterable
Use:
rimcol.iteritems()
Since iteritems()
is a function you must include the parenthesis. Otherwise you get an instance method object instead of an iterator that is returned by that instance method.
None of this actually required row iteration, as Ryan G pointed out in the comments. I think this (untested!) code is equivalent.
convert = (2*np.pi)/365
sinmax = 91
def get_RIMsin(df, obs, rimcol):
"""dataframe, obs column,rim column"""
amplitude = df['Mmedian'].ptp()/2
startday = obs.idxmax().dayofyear
diff = sinmax - startday
adjday = rimcol + diff
adjsin = np.sin(adjday * convert)
df['RIMsine'] = adjsin
return df
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With