pandas AttributeError: 'Series' 对象没有属性 'items'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31444836/
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 23:37:44  来源:igfitidea点击:

AttributeError: 'Series' object has no attribute 'items'

pythonpython-2.7pandasseriesattributeerror

提问by Carolina S

I'm trying to work with a script a colleague wrote.

我正在尝试使用同事编写的脚本。

This part of the script is working fine:

脚本的这部分工作正常:

xl = pd.ExcelFile(path + WQ_file)
sheet_names = xl.sheet_names

df = pd.read_excel(path + WQ_file, sheetname = 'Chemistry Output Table', skiprows = [0,1,2,4,5,6,7], 
               index_col = [0,1], na_values = ['', 'na', '-'])
df.index.names = ['Field_ID', 'Date_Time']

header = pd.read_excel(path + WQ_file, sheetname = 'header data',  
               index_col = [0], na_values = ['', 'na', ' - '])
header_dict = {ah: header['name_short'].loc[ah] for ah in header.index}

analytes_excel = pd.read_excel(path + WQ_file, sheetname = 'analytes', columns = 'name')
analytes_list = [item for sublist in analytes_excel.values.tolist() for item in sublist]
analytes = [header['name_short'].loc[x] for x in analytes_list]    

But this part isn't:

但这部分不是:

# Clean up the data and report "less than" as half of the LOR
df2 = df.copy()
for col in df2.columns:
x = []
for (a, b) in df2[col].items():
    if b == " - ":
        b = np.nan
    try:
        b = float(b)
    except:
        b = float(b.strip('< '))/2
    x.append(b)
df2[col] = x

I get the following error:

我收到以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-80ad8c096fc0> in <module>()
  4 for col in df2.columns:
  5     x = []
 ----> 6     for (a, b) in df2[col].items():
  7         if b == " - ":
  8             b = np.nan

 C:\Users\SardellaC\AppData\Local\Continuum\Anaconda\lib\site-packages\pandas\core\generic.pyc in __getattr__(self, name)
 1938 
 1939         if name in self._internal_names_set:
-> 1940             return object.__getattribute__(self, name)
 1941         elif name in self._metadata:
 1942             return object.__getattribute__(self, name)

 AttributeError: 'Series' object has no attribute 'items'

It might be something to do with different versions of Python used. I'm not familiar at all with Python and would appreciate if someone can point me in the right direction.

这可能与使用的不同 Python 版本有关。我对 Python 一点都不熟悉,如果有人能指出我正确的方向,我将不胜感激。

回答by Kathirmani Sukumar

Use iteritems()instead of items()while iterating through a pandas series as

在迭代Pandas系列时使用iteritems()代替items()

for (a, b) in df2[col].iteritems():
    x = []
    ....

But iterating through each row is a very slow process for large data set. You can simply that part of code by using .apply()function. Let me know if you need to simplify the code.

但是对于大数据集,遍历每一行是一个非常缓慢的过程。您可以使用.apply()函数来简化那部分代码。如果您需要简化代码,请告诉我。