【问题标题】:Appending DataFrame to empty DataFrame in {Key: Empty DataFrame (with columns)}将 DataFrame 附加到 {Key: Empty DataFrame (with columns)} 中的空 DataFrame
【发布时间】:2021-10-17 21:20:07
【问题描述】:

我很难理解这一点。

我有一个常规 df(与 dict 中的空 df 相同的列)和一个空 df,它是字典中的一个值(字典中的键是基于某些输入的变量,所以可以只是一个键/值对或多个键/值对 - 认为这可能是相关的)。 dict结构本质上是:

{key: [[Empty DataFrame
Columns: [list of columns]
Index: []]]}

我正在使用以下代码尝试添加数据:

dict[key].append(df, ignore_index=True)

我得到的错误是:

temp_dict[product_match].append(regular_df, ignore_index=True)
TypeError: append() takes no keyword arguments

这个错误是因为我错误地指定了我试图将 df 附加到的值(就像我试图将 df 附加到键而不是在这里)还是其他什么?

【问题讨论】:

  • 你能举一个输入字典的例子吗?
  • 抱歉回复晚了,输入基于目录端点中的文件数,这些成为键(在对路径进行一些修改后)。这些值就是提到的空dfs。

标签: python-3.x pandas dictionary


【解决方案1】:

也许您在dict[key] 有一个空列表?

请记住,“追加”列表方法(与 Pandas 数据框一不同)仅接收一个参数: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html

【讨论】:

    【解决方案2】:

    您的字典在键处包含一个列表列表,我们可以在显示的输出中看到这一点:

    {key: [[Empty DataFrame Columns: [list of columns] Index: []]]}
    #     ^^ list starts                                        ^^ list ends
    

    出于这个原因,dict[key].append 正在调用 list.append,正如 @nandoquintana 所述。

    要附加到 DataFrame 访问列表中的特定元素:

    temp_dict[product_match][0][0].append(df, ignore_index=True)
    

    注意没有inplace 版本的appendappend 总是产生一个新的DataFrame:

    示例程序:

    import numpy as np
    import pandas as pd
    
    temp_dict = {
        'key': [[pd.DataFrame()]]
    }
    
    product_match = 'key'
    
    np.random.seed(5)
    df = pd.DataFrame(np.random.randint(0, 100, (5, 4)))
    
    temp_dict[product_match][0][0].append(df, ignore_index=True)
    print(temp_dict)
    

    输出(temp_dict 未更新):

    {'key': [[Empty DataFrame
    Columns: []
    Index: []]]}
    

    需要将新的 DataFrame 分配到正确的位置。

    一个新变量:

    some_new_variable = temp_dict[product_match][0][0].append(df, ignore_index=True)
    

    some_new_variable

        0   1   2   3
    0  99  78  61  16
    1  73   8  62  27
    2  30  80   7  76
    3  15  53  80  27
    4  44  77  75  65
    

    或返回列表:

    temp_dict[product_match][0][0] = (
        temp_dict[product_match][0][0].append(df, ignore_index=True)
    )
    

    temp_dict

    {'key': [[    0   1   2   3
    0  99  78  61  16
    1  73   8  62  27
    2  30  80   7  76
    3  15  53  80  27
    4  44  77  75  65]]}
    

    假设 DataFrame 实际上是一个空的 DataFrame,append 是不必要的,因为只需将键处的值更新为 DataFrame 可以工作:

    temp_dict[product_match] = df
    

    temp_dict

    {'key':     0   1   2   3
    0  99  78  61  16
    1  73   8  62  27
    2  30  80   7  76
    3  15  53  80  27
    4  44  77  75  65}
    

    或者如果需要列表列表:

    temp_dict[product_match] = [[df]]
    

    temp_dict

    {'key': [[    0   1   2   3
    0  99  78  61  16
    1  73   8  62  27
    2  30  80   7  76
    3  15  53  80  27
    4  44  77  75  65]]}
    

    【讨论】:

      猜你喜欢
      • 2019-03-22
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-11
      相关资源
      最近更新 更多