Pandas Groupby 值范围 [英] Pandas Groupby Range of Values
问题描述
在 Pandas 中是否有一种简单的方法可以在一系列值增量上调用 groupby
?例如,给出下面的示例,我可以使用 0.155
增量对 B
列进行分组和分组,例如,B
列中的前几个组> 分为 '0 - 0.155, 0.155 - 0.31 ...`
将 numpy 导入为 np将熊猫导入为 pddf=pd.DataFrame({'A':np.random.random(20),'B':np.random.random(20)})甲乙0 0.383493 0.2507851 0.572949 0.1395552 0.652391 0.4019833 0.214145 0.6969354 0.848551 0.516692
或者,我可以首先按这些增量将数据分类到一个新列中,然后使用 groupby
来确定任何可能适用于 A
列的相关统计数据?>
您可能对 pd.cut
:
Is there an easy method in pandas to invoke groupby
on a range of values increments? For instance given the example below can I bin and group column B
with a 0.155
increment so that for example, the first couple of groups in column B
are divided into ranges between '0 - 0.155, 0.155 - 0.31 ...`
import numpy as np
import pandas as pd
df=pd.DataFrame({'A':np.random.random(20),'B':np.random.random(20)})
A B
0 0.383493 0.250785
1 0.572949 0.139555
2 0.652391 0.401983
3 0.214145 0.696935
4 0.848551 0.516692
Alternatively I could first categorize the data by those increments into a new column and subsequently use groupby
to determine any relevant statistics that may be applicable in column A
?
You might be interested in pd.cut
:
>>> df.groupby(pd.cut(df["B"], np.arange(0, 1.0+0.155, 0.155))).sum()
A B
B
(0, 0.155] 2.775458 0.246394
(0.155, 0.31] 1.123989 0.471618
(0.31, 0.465] 2.051814 1.882763
(0.465, 0.62] 2.277960 1.528492
(0.62, 0.775] 1.577419 2.810723
(0.775, 0.93] 0.535100 1.694955
(0.93, 1.085] NaN NaN
[7 rows x 2 columns]
这篇关于Pandas Groupby 值范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!