简体   繁体   English

在python中装箱

[英]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), 由于平均时差为20分钟((30分钟+ 10分钟)/ 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. 它需要在第一条记录上加上平均时间,如果结果时间在数据中,则它属于bin 1,否则属于bin0。然后继续。

 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. 这将按您的预期创建“绑定”,有条件的话,您可以正确地将“时间”创建为日期时间。

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

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