在函数内迭代 Pandas 系列的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24207178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:09:55  来源:igfitidea点击:

Iterating through rows of a Pandas series within a function

pythonpython-2.7pandas

提问by Jason

I'm writing a function part of which should iterate through the rows of a Series. The function should iterate through rows of a DataFramecolumn passed to it i.e. df['col'], however when I try to use .iterrowsI get an error that a Seriesdoesn't have that attribute and using .iteritemsproduces 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.

我正在编写一个函数部分,它应该遍历 a 的行Series。该函数应该遍历DataFrame传递给它的列的行,即 df['col'],但是当我尝试使用时,.iterrows我收到一个错误,即 aSeries没有该属性,并且使用.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

采纳答案by Dan Allan

None of this actually required row iteration, as Ryan G pointed out in the comments. I think this (untested!) code is equivalent.

正如 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

回答by gradi3nt

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.

由于iteritems()是一个函数,您必须包含括号。否则,您将获得一个实例方法对象,而不是该实例方法返回的迭代器。