gpt4 book ai didi

python - AttributeError: 'Series' 对象没有属性 'items'

转载 作者:行者123 更新时间:2023-11-28 21:14:42 25 4
gpt4 key购买 nike

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

脚本的这一部分工作正常:

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]

但这部分不是:

# 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

我收到以下错误:

---------------------------------------------------------------------------
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'

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

最佳答案

使用 iteritems() 而不是 items() 迭代 pandas 系列作为

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

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

关于python - AttributeError: 'Series' 对象没有属性 'items',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31444836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com