AttributeError :(“'Series'对象没有属性'iterrows'”,在索引标识符1'处出现)和其他问题

时间:2019-04-19 03:35:39

标签: python pandas

当我尝试将数据帧传递给我创建的函数时,出现了AttributeError。

我创建了一个函数,该函数应该对要传递的数据帧中的一行进行一些基本操作,但前提是其中一个单元格包含某个值。首先发布抱歉,如果它很长。这是相关的代码和错误。

nitrogen.head()

    Identifier 1       Amount   Area 28  Percent Nitrogen d 15N/14N   d15N
0   sediment standard  1.568    23.478   0                -4.046      0
1   sediment standard  1.801    11.230   0                3.660       0
2   sediment standard  1.554    9.591    0                3.391       0
3   1c-50cm            12.322   36.075   0                4.349       0
4   1c-10cm            11.786   35.296   0                5.622       0

def correctNitrogen(data):
    for index, row in data.iterrows():
        if data.loc[index, 'Identifier 1'] == 'sediment standard':
            standardsP += (data['Amount']/data['Area 28'])*0.62
            standardsD += data['d 15N/14N']
            x += 1

    averageP = standardsP/x
    averageD = standardsD/x
    correcter = 4.42-averageD

    for index, row in data.iterrows():
        if data.loc[index, 'Identifier 1'] != 'sediment standard':
             data['Percent Nitrogen'] = averageP*(data['Area 28']/data['Amount'])
             data['d15N'] = data['d 15N/14N']+correcter

nitrogen.apply(correctNitrogen)

AttributeError                            Traceback (most recent call last)
<ipython-input-175-98f6b563e9b6> in <module>()
----> 1 nitrogen.apply(correctNitrogen)
      2 nitrogen.head()

C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
   4260                         f, axis,
   4261                         reduce=reduce,
-> 4262                         ignore_failures=ignore_failures)
   4263             else:
   4264                 return self._apply_broadcast(f, axis)

C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\frame.py in _apply_standard(self, func, axis, ignore_failures, reduce)
   4356             try:
   4357                 for i, v in enumerate(series_gen):
-> 4358                     results[i] = func(v)
   4359                     keys.append(v.name)
   4360             except Exception as e:

<ipython-input-157-ff5a7f145cc2> in correctNitrogen(data)
      2 # corrects it to the standard that is being used
      3 def correctNitrogen(data):
----> 4     for index, row in data.iterrows():
      5         if data.loc[index, 'Identifier 1'] == 'sediment standard':
      6             standardsP += (data['Amount']/data['Area 28'])*nit

C:\ProgramData\Anaconda2\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
   3079             if name in self._info_axis:
   3080                 return self[name]
-> 3081             return object.__getattribute__(self, name)
   3082 
   3083     def __setattr__(self, name, value):

AttributeError: ("'Series' object has no attribute 'iterrows'", u'occurred at index Identifier 1')

现在,我意识到该功能可能还有其他问题,但这是我到目前为止遇到的第一个问题。我觉得这与我如何将数据帧传递给函数有关,但我不知道如何解决。.

1 个答案:

答案 0 :(得分:1)

简短答案-在列/行上调用函数apply,它是pandas.Series,因此会出现错误。

要在不修改功能的情况下进行修复,请致电:

nitrogen = correctNitrogen(nitrogen)

还要注意,您需要从函数返回DataFrame:

def correctNitrogen(data):
    # do stuff
    return data