Python 计算数据帧值的标准偏差

Python 计算数据帧值的标准偏差,python,pandas,statistics,Python,Pandas,Statistics,我目前正在研究一个机器学习模型,我正在进行特征工程。我使用的数据集显示了13家商店70多种商品的销售数量 我已经创建了一些功能,例如基于SKU-ID(它是什么产品)的商品平均价格。我现在想为每个SKU价格制作一个标准偏差的特征 以下是我用于生成所有门店特定产品平均价格的函数,以及用于确定所有门店产品价格标准差的函数: def gen_average_price(train,test,col,price='base_price',name='name'): temp=train.grou

我目前正在研究一个机器学习模型,我正在进行特征工程。我使用的数据集显示了13家商店70多种商品的销售数量

我已经创建了一些功能,例如基于SKU-ID(它是什么产品)的商品平均价格。我现在想为每个SKU价格制作一个标准偏差的特征

以下是我用于生成所有门店特定产品平均价格的函数,以及用于确定所有门店产品价格标准差的函数:

 def gen_average_price(train,test,col,price='base_price',name='name'):
    temp=train.groupby(col)[price].mean().reset_index().rename(columns={price:name})
    train=pd.merge(train,temp,how='left',on=col)
    test=pd.merge(test,temp,how='left',on=col)
    train[name].fillna(np.median(temp[name]),inplace=True)
    test[name].fillna(np.median(temp[name]),inplace=True)
    return train,test

 def gen_std_price(train,test,col,price='base_price',name='name'):
    temp=train.groupby(col)[price].mean().reset_index().rename(columns={price:name})
    train=pd.merge(train,temp,how='left',on=col)
    test=pd.merge(test,temp,how='left',on=col)
    s = np.median(temp[name])
    train[name].fillna(statistics.stdev(s), inplace = True)
    test[name].fillna(statistics.stdev(s), inplace = True)
    return train,test
    
这是我用来获取特定产品平均价格的函数,也是获取产品价格标准差的函数。上述平均价格函数(gen_average_price)按预期工作,但是,当我试图使用标准偏差函数获取产品价格的标准偏差时,我遇到以下错误:

#Generating std dev of prices per sku-id
train,test = gen_std_price(train,test,col=['sku_id'],price='base_price',name='price_sku_id_stdev') 

TypeError: 'numpy.float64' object is not iterable
有人能帮我解释为什么会出现这种情况和/或如何解决它吗

以下是有关所用数据集的更多信息:

train.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 150150 entries, 0 to 150149
Data columns (total 16 columns):
 #   Column              Non-Null Count   Dtype  
---  ------              --------------   -----  
 0   record_ID           150150 non-null  int64  
 1   week                150150 non-null  object 
 2   store_id            150150 non-null  int64  
 3   sku_id              150150 non-null  int64  
 4   total_price         150150 non-null  float64
 5   base_price          150150 non-null  float64
 6   is_featured_sku     150150 non-null  int64  
 7   is_display_sku      150150 non-null  int64  
 8   units_sold          150150 non-null  int64  
 9   count_id_sku_store  150150 non-null  float64
 10  count_id_sku        150150 non-null  float64
 11  count_id_store      150150 non-null  float64
 12  price_sku_store     150150 non-null  float64
 13  price_to_sku_store  150150 non-null  float64
 14  price_store_id      150150 non-null  float64
 15  price_sku_id        150150 non-null  float64
dtypes: float64(9), int64(6), object(1)
memory usage: 19.5+ MB

test.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 13860 entries, 0 to 13859
Data columns (total 15 columns):
 #   Column              Non-Null Count  Dtype  
---  ------              --------------  -----  
 0   record_ID           13860 non-null  int64  
 1   week                13860 non-null  object 
 2   store_id            13860 non-null  int64  
 3   sku_id              13860 non-null  int64  
 4   total_price         13860 non-null  float64
 5   base_price          13860 non-null  float64
 6   is_featured_sku     13860 non-null  int64  
 7   is_display_sku      13860 non-null  int64  
 8   count_id_sku_store  13860 non-null  float64
 9   count_id_sku        13860 non-null  float64
 10  count_id_store      13860 non-null  float64
 11  price_sku_store     13860 non-null  float64
 12  price_to_sku_store  13860 non-null  float64
 13  price_store_id      13860 non-null  float64
 14  price_sku_id        13860 non-null  float64
dtypes: float64(9), int64(5), object(1)
memory usage: 1.7+ MB
train.info()
INT64索引:150150个条目,0到150149
数据列(共16列):
#列非空计数数据类型
---  ------              --------------   -----  
0记录\u ID 150150非空int64
1周150150非空对象
2存储单元id 150150非空int64
3 sku_id 150150非空int64
4总价格150150非零浮动64
5基价150150非空浮动64
6是sku 150150非空int64
7是否显示sku 150150非空int64
8个单元,售出150150个非空整数64
9计数\u id\u sku\u存储150150非空浮点64
10计数\u标识\u sku 150150非空浮点64
11计数id存储150150非空浮点64
12 price_sku_store 150150非空浮动64
13价格到库存单位商店150150非空浮动64
14价格商店id 150150非空浮动64
15价格单品标识150150非空浮动64
数据类型:float64(9)、int64(6)、object(1)
内存使用率:19.5+MB
test.info()
INT64索引:13860个条目,0到13859
数据列(共15列):
#列非空计数数据类型
---  ------              --------------  -----  
0记录\u ID 13860非空int64
1周13860非空对象
2存储单元id 13860非空int64
3 sku_id 13860非空int64
4总价格13860非零浮动64
5基准价格13860非空浮动64
6是sku 13860非空int64吗
7是否显示sku 13860非空int64
8计数\u id\u sku\u存储13860非空浮点64
9计数\u标识\u sku 13860非空浮点64
10计数\u标识\u存储13860非空浮点64
11 price_sku_store 13860非空浮动64
12价格到库存单位商店13860非空浮动64
13价格商店id 13860非空浮动64
14价格单品识别号13860非空浮动64
数据类型:float64(9)、int64(5)、object(1)
内存使用率:1.7+MB

另一种方法是,您可以使用
train.descripe()
对数据集进行完整描述。它将返回计数、平均值、标准偏差、中值、第一个四分位数、第三个四分位数和最大值

示例

df = pd.DataFrame({"a":range(0,10),"b":range(10,20)})
df.describe()