简体   繁体   中英

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?

For example, 0.031190 would be in a group of values between 0.0296305 and 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%.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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