简体   繁体   English

如何在熊猫python中按每个值的百分比范围分组

[英]How to groupby a percentage range of each value in pandas python

If I have a dataframe of the format: 如果我有一个格式的数据框:

          date              value
2018-10-31 23:45:00         0.031190
2018-11-01 00:00:00         0.031211
2018-11-01 00:15:00         0.031201
2018-11-01 00:30:00         0.031203
2018-11-01 00:45:00         0.031186
2018-11-01 01:00:00         0.031208
2018-11-01 01:15:00         0.031191
2018-11-01 01:30:00         0.031170
2018-11-01 01:45:00         0.031155
2018-11-01 02:00:00         0.031146
2018-11-01 02:15:00         0.031176
2018-11-01 02:30:00         0.031178
2018-11-01 02:45:00         0.031163
2018-11-01 03:00:00         0.031187
2018-11-01 03:15:00         0.031140
2018-11-01 03:30:00         0.031165
2018-11-01 03:45:00         0.031166
2018-11-01 04:00:00         0.031182
2018-11-01 04:15:00         0.031155
2018-11-01 04:30:00         0.031145
2018-11-01 04:45:00         0.031177
2018-11-01 05:00:00         0.031189
2018-11-01 05:15:00         0.031183
2018-11-01 05:30:00         0.031175
2018-11-01 05:45:00         0.031184
2018-11-01 06:00:00         0.031174
2018-11-01 06:15:00         0.031167
2018-11-01 06:30:00         0.031161
2018-11-01 06:45:00         0.031163
2018-11-01 07:00:00         0.031211
2018-11-01 07:15:00         0.031183
2018-11-01 07:30:00         0.031156
2018-11-01 07:45:00         0.031142
2018-11-01 08:00:00         0.031154
2018-11-01 08:15:00         0.031152
2018-11-01 08:30:00         0.031137
2018-11-01 08:45:00         0.031142
2018-11-01 09:00:00         0.031155
2018-11-01 09:15:00         0.031145
2018-11-01 09:30:00         0.031154
2018-11-01 09:45:00         0.031140
2018-11-01 10:00:00         0.031146
2018-11-01 10:15:00         0.031149
2018-11-01 10:30:00         0.031164
2018-11-01 10:45:00         0.031172
2018-11-01 11:00:00         0.031162
2018-11-01 11:15:00         0.031141
2018-11-01 11:30:00         0.031165
2018-11-01 11:45:00         0.031174
2018-11-01 12:00:00         0.031180

How do I segment the data into groups of a 5% difference in value? 如何将数据分成价值相差5%的组?

For example, 0.031190 would be in a group of values between 0.0296305 and 0.0327495. 例如,0.031190将位于0.0296305和0.0327495之间的一组值中。 If a value is within multiple groups that is fine - in fact it is expected. 如果一个值在多个组中可以-实际上是预期的。 If a value is not anywhere near any other values, then it will just be by itself. 如果某个值与其他任何值都不相近,那么它本身就是唯一。

based on the data you provided something like this would work; 根据您提供的数据,类似的方法会起作用;

assuming you would need the range divided in 20 bins of 5%. 假设您需要将范围分为5%的20个bin。

df['binned'] = pd.qcut(df['value'], 20)

df = df.groupby('binned')['value'].count()

print(df.head())

binned
(0.031127000000000002, 0.03114]    3
(0.03114, 0.031142]                3
(0.031142, 0.031145]               2
(0.031145, 0.031148]               2
(0.031148, 0.031154]               4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM