Using concat for a dictionary I get the error: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"
Using concat for a dictionary I get the error: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"
我有一个包含数据框的字典,看起来像这样:
dataframes = {'Df_20100101': DataFrame, 'Df_20100102': DataFrame, 'Df_20100103': DataFrame}
所有数据框都有相同的变量(价格、交易量和日期)和相同的索引。我想将所有不同的数据帧放入 1 个数据帧中。我使用以下代码:
df = pd.concat([pd.concat(v,ignore_index=True) for k,v in dataframes.items()])
但是,我得到一个错误:the first argument must be iterable of pandas objects, you passed an object of type "DataFrame"
。
是不是所有变量的Index都一样?
谁能帮帮我?
谢谢!
您正在对字典中的单个数据帧调用 pd.concat
(您嵌套了 pd.concat()
调用)。相反,您只需要列表理解将数据帧收集到一个列表中并调用 .concat()
:
df = pd.concat([v for _, v in dataframes.items()], ignore_index=True)
for k, v
也是完全有效的,但是习惯上使用 _
表示您要丢弃的值,并且您不使用此处的键。
您也可以只使用 .values()
:
df = pd.concat(dataframes.values(), ignore_index=True)
我有一个包含数据框的字典,看起来像这样:
dataframes = {'Df_20100101': DataFrame, 'Df_20100102': DataFrame, 'Df_20100103': DataFrame}
所有数据框都有相同的变量(价格、交易量和日期)和相同的索引。我想将所有不同的数据帧放入 1 个数据帧中。我使用以下代码:
df = pd.concat([pd.concat(v,ignore_index=True) for k,v in dataframes.items()])
但是,我得到一个错误:the first argument must be iterable of pandas objects, you passed an object of type "DataFrame"
。
是不是所有变量的Index都一样?
谁能帮帮我?
谢谢!
您正在对字典中的单个数据帧调用 pd.concat
(您嵌套了 pd.concat()
调用)。相反,您只需要列表理解将数据帧收集到一个列表中并调用 .concat()
:
df = pd.concat([v for _, v in dataframes.items()], ignore_index=True)
for k, v
也是完全有效的,但是习惯上使用 _
表示您要丢弃的值,并且您不使用此处的键。
您也可以只使用 .values()
:
df = pd.concat(dataframes.values(), ignore_index=True)