gpt4 book ai didi

python - Pandas df.at() 引发 AttributeError : 'BlockManager' object has no attribute 'T'

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:39 28 4
gpt4 key购买 nike

我有一个相对庞大的数据框。我正在尝试迭代到每一行并根据特定列值更新列(基本上是尝试循环查找直到无法更新其他列)

我有以下内容:

df = the huge dataframe (1K to 10K+ rows x 51 cols)

has_update = True
while has_update:
has_update = False

for_procdf = df.loc[df['Incident Group ID'] == '-']

for i, row in for_procdf.iterrows():
#Check if the row's parent ticket id is an existing ticket id in the bigger df
resultRow = df.loc[df['Ticket ID'] == row['Parent Ticket ID']]
resultCount = len(resultRow.index)
if resultCount == 1:
IncidentGroupID = resultRow.iloc[0]['Incident Group ID']
if IncidentGroupID != '-':
df.at[i, "Incident Group ID"] = IncidentGroupID
has_update = True

当我执行脚本时,出现以下回溯错误:

Traceback (most recent call last):
File "./sdm.etl.py", line 76, in <module>
main()
File "./sdm.etl.py", line 28, in main
fillIncidentGroupID(sdmdf.df)
File "./sdm.etl.py", line 47, in fillIncidentGroupID
df.at[i, "Incident Group ID"] = IncidentGroupID
File "/usr/local/lib/python3.6/site-packages/pandas/core/indexing.py", line 2159, in __setitem__
self.obj._set_value(*key, takeable=self._takeable)
File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 2580, in _set_value
series = self._get_item_cache(col)
File "/usr/local/lib/python3.6/site-packages/pandas/core/generic.py", line 2490, in _get_item_cache
res = self._box_item_values(item, values)
File "/usr/local/lib/python3.6/site-packages/pandas/core/frame.py", line 3096, in _box_item_values
return self._constructor(values.T, columns=items, index=self.index)
AttributeError: 'BlockManager' object has no attribute 'T'

但是创建类似的场景不会返回错误

>>> qdf = pd.DataFrame([[0, 2, 3], [0, 4, 1], [10, 20, 30], [10, 13, 17]], index=[0,1,2,3], columns=['Ab 1', 'Bc 2', 'Cd 3'])
>>> qdf
Ab 1 Bc 2 Cd 3
0 0 2 3
1 0 4 1
2 10 20 30
3 10 13 17
>>>
>>> qdf1 = qdf.loc[qdf['Ab 1'] == 0]
>>> qdf1
Ab 1 Bc 2 Cd 3
0 0 2 3
1 0 4 1
>>>
>>> for i, row in qdf1.iterrows():
... qdf.at[i, 'Ab 1'] = 10
...
>>>
>>> qdf
Ab 1 Bc 2 Cd 3
0 10 2 3
1 10 4 1
2 10 20 30
3 10 13 17

我的实现似乎有什么问题?

最佳答案

发现,Nihal是的,错误是由重复的列名引起的。我的数据框太大,我不小心有一个重复的列名。现在一切正常。离开代码,休息和吃饭的时间让我看到了重复的列。干杯!

下面是我的数据框的列。 “RCA Group ID”在末尾有重复。

['Incident Group ID', 'RCA Group ID', 'Parent Ticket ID', 'Ticket ID', ..., 'RCA Group ID', 'Is Sector Down', 'Relationship Type']

关于python - Pandas df.at() 引发 AttributeError : 'BlockManager' object has no attribute 'T' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54415673/

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