我有一个大数据框:
peak.count purity.score material
0 10.0 0.134814 ADB
1 10.0 0.134814 ADB
2 17.0 0.116754 ADB
3 17.0 0.116754 ADB
4 17.0 0.102921 ADB
... ... ... ...
1269 14.0 0.166039 SSA
1270 14.0 0.166039 SSA
1271 14.0 0.166039 SSA
1272 12.0 0.169396 SSA
1273 12.0 0.169396 SSA
1274 12.0 0.169396 SSA
我很好奇按范围对purity.score进行分组,然后对范围内的这些值进行计数。例如,如果我的值中有 15 个介于 0.1 和 0.2 之间,我希望输出在 1 处反射(reflect) 15。我尝试过将 value_counts 与 numpy 范围结合使用,但不计算组内的值:
首先我这样做:s = pd.Series(df['purity.score'])
pd.value_counts(s).reindex(np.arange(0,1,0.1)).fillna(0)
0.0 362.0
0.1 0.0
0.2 0.0
0.3 0.0
0.4 0.0
0.5 0.0
0.6 0.0
0.7 0.0
0.8 0.0
0.9 0.0
如何对这些值进行分组?注意:我希望使用此表来提供 API 以呈现 javascript 直方图,但不希望使用 Bokeh 或 Matplotlib。我需要访问 table 。
最佳答案
您可以使用cut
来做到这一点功能:
df.groupby(pd.cut(df['purity.score'], bins=10)).count()
此处,cut
将 df['purity.score']
分为其选择的 10 个 bin,但您可以通过传递数组来定义 bin 边界。
关于python - 使用 Pandas 如何计算数据组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36777442/