如何使用Pandas的Quantile打印系列中超过75%的数值
给定一个系列,任务是使用Python中的Pandas打印所有高于第75百分位数的元素。有一个系列的数据,我们必须找到该系列对象中所有数值大于第75百分位数的数值。
步骤:
- 创建一个任何数据集的系列对象
- 我们将使用pandas系列的量化函数来计算第75百分位数
- 我们将应用for循环来迭代系列对象的所有值
- 在for循环中,我们将检查该值是否大于在step(2)中计算的第75个四分位值,如果大于,则打印出来。
代码:
# importing pandas module
import pandas as pd
# importing numpy module
import numpy as np
# Making an array
arr = np.array([42, 12, 72, 85, 56, 100])
# creating a series
Ser1 = pd.Series(arr)
# printing this series
print(Ser1)
# calculating quantile/percentile value
quantile_value = Ser1.quantile(q=0.75)
# printing quantile/percentile value
print("75th Percentile is:", quantile_value)
print("Values that are greater than 75th percentile are:")
# Running a loop and
# printing all elements that are above the
# 75th percentile
for val in Ser1:
if (val > quantile_value):
print(val)
输出:
0 42
1 12
2 72
3 85
4 56
5 100
dtype: int32
75th Percentile is: 81.75
Values that are greater than 75th percentile are:
85
100
解释:
我们从一个nd数组中制作了一个系列对象,并使用quantile()方法在给定的系列对象中找到数据的75%quantile或75th percentile值,然后使用for循环找出系列中所有高于75th percentile的值。