Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pandas Groupby熊猫定制系列?_Pandas_Pandas Groupby - Fatal编程技术网

Pandas Groupby熊猫定制系列?

Pandas Groupby熊猫定制系列?,pandas,pandas-groupby,Pandas,Pandas Groupby,我有一个数据帧: ID Value 1 1 2 2 3 1 4 1 5 2 6 3 7 4 8 5 9 10 10 15 我想按我的值分组并计算ID,在自定义范围内:=10 结果如下: Value ID <=2

我有一个数据帧:

 ID       Value
  1           1  
  2           2
  3           1    
  4           1
  5           2
  6           3
  7           4
  8           5  
  9          10  
 10          15
我想按我的值分组并计算ID,在自定义范围内:=10

结果如下:

 Value      ID
 <=2         5
 3-9         3
 >= 10       2
值ID
= 10       2
使用和

将numpy导入为np
标签=['=10']
bins=[0,2,9,np.inf]
df.groupby(pd.cut(df['Value'],bin,labels=labels)).size()到框架('ID'))
#输出
身份证件
价值
=10     2
使用和

将numpy导入为np
标签=['=10']
bins=[0,2,9,np.inf]
df.groupby(pd.cut(df['Value'],bin,labels=labels)).size()到框架('ID'))
#输出
身份证件
价值
=10     2

我认为你不需要groupby

labels = ['<=2', '3-9', '>=10']
bins = [0,2,9, np.inf]
pd.cut(df['Value'],bins=bins,labels=labels).value_counts().reset_index()
#out[]
index   Value
<=2     5
3-9     3
>=10    2
labels=['=10']
bins=[0,2,9,np.inf]
pd.cut(df['Value'],bin=bin,labels=labels)。Value\u counts().reset\u index()
#out[]
索引值
=10    2

我认为你不需要groupby

labels = ['<=2', '3-9', '>=10']
bins = [0,2,9, np.inf]
pd.cut(df['Value'],bins=bins,labels=labels).value_counts().reset_index()
#out[]
index   Value
<=2     5
3-9     3
>=10    2
labels=['=10']
bins=[0,2,9,np.inf]
pd.cut(df['Value'],bin=bin,labels=labels)。Value\u counts().reset\u index()
#out[]
索引值
=10    2

如何将结果重命名为我想要的?@homsand您想在此处重命名什么?例如:
(0.0,2.0]
@homsand Updated。如何将结果重命名为我想要的?@homsand您想在此处重命名什么?例如:
(0.0,2.0]
@homsand Updated。