英文:
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
>>> 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:
>>> 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论