【发布时间】:2021-12-25 16:35:01
【问题描述】:
我正在使用 pandas 运行一个 for 循环,检查是否已创建另一个同名的 DataFrame。如果已创建,则只需将值附加到相应的列。如果尚未创建,则创建 df 并将值附加到命名列。
dflistglobal = []
####
For loop that generate a, b, and c variables every time it runs.
####
###
The following code runs inside the for loop, so that everytime it runs, it should generate a, b, and c, then check if a df has been created with a specific name, if yes, it should append the values to that "listname". If not, it should create a new list with "listname". List name changes everytime I run the code, and it varies but can be repeated during this for loop.
###
if listname not in dflistglobal:
dflistglobal.append(listname)
listname = pd.DataFrame(columns=['a', 'b', 'c'])
listname = listname.append({'a':a, 'b':b, 'c':c}, ignore_index=True)
else:
listname = listname.append({'a':a, 'b':b, 'c':c}, ignore_index=True)
我收到以下错误:
File "test.py", line 150, in <module>
functiontest(image, results, list)
File "test.py", line 68, in funtiontest
listname = listname.append({'a':a, 'b':b, 'c':c}, ignore_index=True)
AttributeError: 'str' object has no attribute 'append'
最初的 if 语句运行良好,但 else 语句会导致问题。
【问题讨论】:
标签: python pandas string loops append