Python计算每个分位数和预设四分位数。

huangapple go评论81阅读模式
英文:

Python Count per quantiles and pre set quartiles

问题

我想知道是否有一种代码,可以计算我的数据集中有多少比例的数据落在25%,50%,75%这些位置,并且是否可以手动调整四分位数,例如20%,40%,60%,80%等。

Out[1]: 
              ALC (mins)
       count  303.000000
       mean   24.928812
       std    18.316443
       min    3.120000
       25%    12.680000
       50%    20.770000
       75%    30.535000
       max    143.180000
```

<details>
<summary>英文:</summary>

I want to know if there is a code wherein I can count how many in my dataset goes to 25%, 50%, 75% and if there is a way to adjust the quartiles manually like 20% 40% 60% 80%, etc. 

````python
Out[1]: 
              ALC (mins)
       count  303.000000
       mean   24.928812
       std    18.316443
       min    3.120000
       25%    12.680000
       50%    20.770000
       75%    30.535000
       max    143.180000

</details>


# 答案1
**得分**: 2

使用`percentiles`可选参数

```python
&gt;&gt;&gt; df.describe(percentiles = [0.2, 0.4, 0.6, 0.8])
           (mins)
count    8.000000
mean    69.566282
std    104.255870
min      3.120000
20%     14.934577
40%     20.279289
50%     22.849406
60%     26.050050
80%     98.122000
max    303.000000
```

用于计算每个四分位数中的元素数量

```python
np.linspace(1, len(df), 5, dtype = np.int64)  # 除以5,因为它从最小值开始,然后是25%,50%,75%,最后是最大值。
```

<details>
<summary>英文:</summary>

Use `percentiles` optional argument:

    &gt;&gt;&gt; df.describe(percentiles = [0.2, 0.4, 0.6, 0.8])
               (mins)
    count    8.000000
    mean    69.566282
    std    104.255870
    min      3.120000
    20%     14.934577
    40%     20.279289
    50%     22.849406
    60%     26.050050
    80%     98.122000
    max    303.000000

For counting how much elements will per quartile:

    np.linspace(1, len(df), 5, dtype = np.int64)  # division by 5 since it starts from min, to 25%, 50%, 75%, then max.



</details>



huangapple
  • 本文由 发表于 2020年1月4日 01:42:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定