函数字典返回的意外结果

时间:2018-10-27 09:02:29

标签: python pandas dictionary

我正在尝试使用dict理解来创建函数字典。在此示例中,我正在计算分位数。结果与预期不符:

import pandas as pd
import numpy as np

# Fake data 
np.random.seed(0)
measures = pd.Series(np.random.normal(20, 4, 100))

# Required quantiles
quantile_values = np.arange(0,1,0.2)

使用值循环:

# Get quantiles  
for quantile_value in quantile_values:
    print(measures.quantile(quantile_value))

# 9.788040736663685
# 16.570255914888975
# 19.222505537912717
# 21.285241407760992
# 23.821594165268856

使用功能字典;

# Create a dict of quantile functions 
quantiles_functions = {
    'quantile_{}'.format(round(quantile_value, 2)):    #  Format key with the quantile value
     lambda series: series.quantile(quantile_value)    #  Create the quantile function
     for quantile_value in quantile_values}            #  For each quantile in the range  

# Get quanties u sing dict 
for fn_name in quantiles_functions:
    print(quantiles_functions[fn_name](measures))

# 23.821594165268856
# 23.821594165268856
# 23.821594165268856
# 23.821594165268856
# 23.821594165268856

0 个答案:

没有答案