我正在编写一个函数部分,它应该迭代Series
的行。该函数应该迭代传递给它的DataFrame
列的行,即df ['col'],但是当我尝试使用.iterrows
时,我得到一个Series
没有的错误拥有该属性并使用.iteritems
产生以下错误。有没有其他方法来遍历列的行?我需要能够访问索引和列值。
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
答案 0 :(得分:10)
使用:
rimcol.iteritems()
由于iteritems()
是一个函数,因此必须包含括号。否则,您将获得实例方法对象,而不是该实例方法返回的迭代器。
答案 1 :(得分:2)
这些实际上都不需要行迭代,正如Ryan G在评论中指出的那样。我认为这个(未经测试的!)代码是等价的。
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