【量化纯GET实战 #21】用 pandas 做分析:把接口数据变成 DataFrame

【量化纯GET实战 #21】用 pandas 做分析:把接口数据变成 DataFrame

pandas 让分析起飞

前面的计算都是手写循环。一旦接入 pandas,切片、统计、分组都变成一行的事。下面把 K 线变成 DataFrame,直接看统计摘要。

python 复制代码
import requests, pandas as pd
BASE = "https://api.momaapi.com"
LICENCE = "你的魔码证书"
bars = requests.get(f"{BASE}/hsstock/history/600519/d/n/{LICENCE}",
                    params={"st":"20250101","et":"20250401"}).json()
df = pd.DataFrame(bars)[["t","o","c","h","l","pc"]]
print(df.head(3).to_string(index=False))
print("\n统计摘要:")
print(df[["c","pc"]].describe().round(3).to_string())

运行结果(真实返回):

text 复制代码
         t     o     c     h     l    pc
2025-09-15 11.70 11.65 11.72 11.63 11.72
2025-09-16 11.66 11.64 11.71 11.61 11.65
2025-09-17 11.64 11.64 11.67 11.58 11.64

统计摘要:
            c      pc
count  50.000  50.000
mean   11.543  11.539
std     0.136   0.140
min    11.320  11.320
25%    11.412  11.402
50%    11.530  11.520
75%    11.648  11.648
max    11.850  11.850

能干更多

DataFrame 之后:df.resample 做周/月聚合、df.rolling(20).mean() 直接算均线、df.corr() 算相关性(第 23 篇)。pandas 是量化的标配,建议尽早熟悉。


魔码云服

相关推荐
三十岁老牛再出发37 分钟前
8月27日总结
python·pandas
Zzzzmo_1 小时前
Spring MVC
java·spring·mvc·cookie/session
Carl_奕然1 小时前
【智能体】Agent的四种设计模式之:React(2026最新版)
javascript·人工智能·python·react.js·设计模式·语言模型
2601_960356381 小时前
金融工程专业大学期间适合考哪些证?就业方向与考证规划
金融
cfm_29141 小时前
Spring核心设计模式
java·spring·设计模式
岁月宁静1 小时前
四、《从零手撸 Agent》 — 流式输出:接住 AI “一个字一个字” 想出来的过程
python·agent
Java小白笔记1 小时前
Java 实现阿里云 OSS 文件上传链路:普通上传、秒传、分片与断点续传
java·开发语言·数据库·spring·阿里云
是吕先森1 小时前
【python】selenium实现web自动化测试
前端·python·selenium
2333!!!!!1 小时前
rocket新手一些常见问题
java·开发语言