简体   繁体   中英

Binning in python

Example

Input have one column .

       Time
       02.10 
       02.40 
       02.50

Output

Since the Ave time difference is 20 min ((30 min+10 min)/2),

I need a data frame which buckets the data by average . It needs to add average time to first record , if the resultant time is there in data then it belongs to bin 1 , otherwise to bin 0. and then continue.

 Desired Output
    Time  - Bin
    02.10 - 1
    02.30 - 0
    02.50 - 1
    03.10 - 0

Thanks in advance.

First, you should always share what you have tried.

Anyways, try this. It should work

mean = df.Time.diff().mean()

start = df.loc[0, 'Time']
end = df.loc[df.shape[1] -1, 'Time']
len = int((end - start)/mean) + 1

timeSeries = [start + i*mean for i in range(len)]

df['Bin'] = 0

df.loc[df['Time'].isin(timeSeries), 'Bin'] = 1

This will create 'Bin' as expected by you, conditional, you create 'Time' properly as datetime.

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