Dataframe 将数据帧拆分为特定列的四分位数

Dataframe 将数据帧拆分为特定列的四分位数,dataframe,Dataframe,我想将一个数据帧分为4个数据帧,分别命名为q1、q2、q3和q4,其中q1应包含所有行,其中特定列(如年龄)在(年龄)分布中处于最低的25%,q2从25%到50%,q3从50%到75%,q4从75%到100%。或者换句话说:我想根据年龄创建4个大小相同的人群 我怎样才能以pythonic的方式做到这一点(目前我正在使用循环,但这可能不是一个很好的解决方案)?不是很漂亮,但有技巧(如果有人感兴趣): df = pd.DataFrame(np.array([[1, 100], [2, 10], [3

我想将一个数据帧分为4个数据帧,分别命名为q1、q2、q3和q4,其中q1应包含所有行,其中特定列(如年龄)在(年龄)分布中处于最低的25%,q2从25%到50%,q3从50%到75%,q4从75%到100%。或者换句话说:我想根据年龄创建4个大小相同的人群


我怎样才能以pythonic的方式做到这一点(目前我正在使用循环,但这可能不是一个很好的解决方案)?

不是很漂亮,但有技巧(如果有人感兴趣):

df = pd.DataFrame(np.array([[1, 100], [2, 10], [3, 1], [4, 50], [5, 43], [6, 61], [7, 99], [7, 11]]), columns=['idx', 'age'])

print(df)

q = df.quantile([0.00, 0.25, 0.50, 0.75, 1.00])

col = 'age'

q1 = df[((df[col]>=q[col][0.00]) & (df[col]<q[col][0.25]))]
q2 = df[((df[col]>=q[col][0.25]) & (df[col]<q[col][0.50]))]
q3 = df[((df[col]>=q[col][0.50]) & (df[col]<q[col][0.75]))]
q4 = df[((df[col]>=q[col][0.75]) & (df[col]<=q[col][1.00]))]
print('----')
print(q1)
print('----')
print(q2)
print('----')
print(q3)
print('----')
print(q4)
   idx  age
0    1  100
1    2   10
2    3    1
3    4   50
4    5   43
5    6   61
6    7   99
7    7   11
----
   idx  age
1    2   10
2    3    1
----
   idx  age
4    5   43
7    7   11
----
   idx  age
3    4   50
5    6   61
----
   idx  age
0    1  100
6    7   99