You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多DataFrame列遍历及条件生成新DataFrame的实现问题

批量生成单证券多指标合并表解决方案

核心思路

遍历目标证券列表里的每只证券,从三个DataFrame中分别提取对应列,横向合并成包含该证券三类指标的新DataFrame,用字典存储结果,完全适配任意长度的securities3列表。

实现代码

import pandas as pd

# 初始化字典,用来存每个证券的合并结果
security_merged_dfs = {}

# 循环处理每只目标证券
for security in securities3:
    # 从三个DataFrame里分别抓对应证券的列,合并成新表
    combined_df = pd.concat([df[security] for df in df_list], axis=1)
    # 给合并后的列命名,对应原DataFrame的类型
    combined_df.columns = ['px', 'SC', 'SMA']
    # 把结果存入字典,用证券名当键,后续调用更方便
    security_merged_dfs[security] = combined_df

使用示例

想要查看某只证券的合并结果,直接通过字典键调用即可:

# 获取SPX Index的合并表并打印前5行
print(security_merged_dfs['SPX Index'].head())

示例输出

px    SC    SMA
2023-01-01  4510.0  0.48  4480.5
2023-01-02  4535.2  0.51  4492.3
2023-01-03  4522.8  0.49  4498.1
2023-01-04  4540.1  0.52  4505.7
2023-01-05  4530.5  0.50  4512.2

内容的提问来源于stack exchange,提问作者bigjdawg43

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 07:22:12