我有一个相对较大的数据框。我试图遍历每一行并根据特定的列值更新一列(基本上是尝试循环查找,直到无法更新其他列为止)
我有以下内容:
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
我的实现似乎有什么问题?
答案 0 :(得分:2)
发现Nihal是正确的,该错误是由重复的列名引起的。我的数据框太大,以至于我不小心有重复的列名。现在一切正常。离代码有点时间,休息和吃东西使我看到重复的列。干杯!
下面是我的数据框的列。 “ RCA组ID” 在结尾附近重复。
['Incident Group ID', 'RCA Group ID', 'Parent Ticket ID', 'Ticket ID', ..., 'RCA Group ID', 'Is Sector Down', 'Relationship Type']
答案 1 :(得分:0)
该错误是由重复的列名引起的
在我的情况下是这样。
您可以使用以下功能快速确定哪些列名称重复。
def get_duplicate_cols(df: pd.DataFrame) -> pd.Series:
return pd.Series(df.columns).value_counts()[lambda x: x>1]