我需要从一个Dictionary中的多个DataFrame中提取相同的列,并将它们保存为一个DataFrame,以便每个列名都被标记为Dictionary中的键。
通过从字典中的每个 DataFrame 中提取“关闭”列,我最终得到以下系列:
约会 | 关闭 |
---|---|
2023-03-06 04:00:00 | 1 |
2023-03-06 05:00:00 | 2 |
2023-03-06 06:00:00 | 2 |
我需要制作一个包含 n 列的 DataFrame,如下所示:
约会 | 间谍 | QQQ | SMH |
---|---|---|---|
2023-03-06 04:00:00 | 1 | 3 | 2 |
2023-03-06 05:00:00 | 2 | 4 | 4 |
2023-03-06 06:00:00 | 3 | 2 | 3 |
代码的数量各不相同。
假设您创建的字典称为
d
,相应的空数据框称为final_df
,您的数据框之一称为spy_df
你可以像这样简单地循环字典:
for idx, val in d.items():
final_df[idx] = val['close'] #idx would be the key value in the dictionary
final_df.index = spy_df.index