首页 > 解决方案 > 在堆栈中查找特殊行的标准差

问题描述

我有一个堆栈如下,我必须找到 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

标签: pythonpandas

解决方案


# 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()]


推荐阅读