【问题标题】:Checking if another DataFrame with same name has been created. Error: 'str' object has no attribute 'append'检查是否已创建另一个具有相同名称的 DataFrame。错误:“str”对象没有属性“append”
【发布时间】: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


    【解决方案1】:

    该错误告诉您 listname 是一个字符串(并且您不能将 DataFrame 附加到字符串)。

    您可能想检查是否在代码中的某处将字符串添加到列表dflistglobal

    编辑:可能的解决方案

    我不确定你是如何命名你的 DataFrame,我不知道你以后如何访问它们。

    您可以将 DataFrame 存储在字典 dict = {"name": df} 中,而不是使用列表。这将使您可以轻松地按名称访问 DataFrame。

    import pandas as pd
    import random
    
    df_dict = {}
    
    # For loop
    for _ in range(10):
        # Logic to get your variables (example)
        a = random.randint(1, 10)
        b = random.randint(1, 10)
        c = random.randint(1, 10)
    
        # # Logic to get your DataFrame name (example)
        df_name = f"dataframe{random.randint(1,10)}"
    
        if df_name not in df_dict.keys():
            # DataFrame with same name does not exist
            new_df = pd.DataFrame(columns=['a', 'b', 'c'])
            new_df = new_df.append({'a':a, 'b':b, 'c':c}, ignore_index=True)
            df_dict[df_name] = new_df
            
        else:
            # DataFrame with same name exists
            updated_df = df_dict[df_name].append({'a':a, 'b':b, 'c':c}, ignore_index=True)
            df_dict[df_name] = updated_df
         
    

    另外,欲了解更多信息,您可能需要访问this question

    我希望它很清楚并且对您有所帮助。

    【讨论】:

    • 是的,我将列表的名称附加到 dflistglobal,但我希望 pandas 会检测到创建了具有相同名称的列表并将其用作附加到它的一种方式.
    【解决方案2】:

    通过不使用 pandas 数据框解决了这个问题。我通过 for 循环循环,为每个 listname 生成一个唯一标识符,然后将 a,b,c,listname 附加到列表中。最后,您将得到一个大的df,可以使用groupby 函数进行过滤。

    不确定这是否对任何人都有帮助,但请避免创建 pandas dfs 并使用列表是最好的方法。

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 2021-11-19
      • 2021-06-26
      • 2015-03-08
      • 1970-01-01
      • 2019-10-06
      • 1970-01-01
      相关资源
      最近更新 更多