python - 在堆栈中查找特殊行的标准差
问题描述
我有一个堆栈如下,我必须找到 A AAPL 标准偏差最低的月份
A
Date
2000-07-03 AAPL 3.290673
ADBE 31.756767
ADI 54.067356
2000-07-04 AAPL 6.660673
ADBE 71.956767
ADI 44.767356
2020-08-07 AAPL 210.399994
ADBE 24.88
ADI 55.09
2020-08-17 AAPL 329.399994
ADBE 64.88
ADI 99.09
2020-09-10 AAPL 70.39
ADBE 14.81
ADI 65.69
2020-09-20 AAPL 140.39
ADBE 83.51
ADI 57.69
解决方案
# sample data
s = """Date,Stock,A
7/3/2000,AAPL,3.290673
7/3/2000,ADBE,31.756767
7/3/2000,ADI,54.067356
7/4/2000,AAPL,6.660673
7/4/2000,ADBE,71.956767
7/4/2000,ADI,44.767356
8/7/2020,AAPL,210.399994
8/7/2020,ADBE,24.88
8/7/2020,ADI,55.09
8/17/2020,AAPL,329.399994
8/17/2020,ADBE,64.88
8/17/2020,ADI,99.09
9/10/2020,AAPL,70.39
9/10/2020,ADBE,14.81
9/10/2020,ADI,65.69
9/20/2020,AAPL,140.39
9/20/2020,ADBE,83.51
9/20/2020,ADI,57.69
"""
df = pd.read_csv(StringIO(s))
# convert to datetime
df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index(['Date', 'Stock'])
# filter frame and reset the index
f = df[df.index.get_level_values(1) == 'AAPL'].reset_index()
# groupby month and year and calc the std of each group
std = f.groupby([f['Date'].dt.month, f['Date'].dt.year])['A'].std()
# return the index of the min std
std.idxmin()
# output is below, which is the month and the year of the lowest std
# (7, 2000)
或者,如果您想重新运行系列更改std.idxmin()
为std[std == std.min()]
推荐阅读
- frida - frida - 在 android apk 中挂钩套接字输入流
- broadcast - TRON 区块链:通过公共节点的 API 创建、签署和广播交易
- r - Function two plots in a 1*2 array
- python - 通过逻辑回归平衡
- javascript - JavaScript for 循环给我一个错误 - 循环对象数组
- python - 从另一个数据框创建 Pandas 高级列
- html - 您可以完全使用 XML 和 XSLT 构建网站吗?
- oracle - Oracle 函数不会编译
- ios - SwiftUI:如何创建垂直压缩时缩小的单行标签?
- java - 练习 JUnit 比较字符串的奇怪行为