简体   繁体   English

计算熊猫范围内的元素

[英]Count element within a range in panda

I have a table where in the first column there are integer numbers (7,8,17,467 etc) indicating the seconds and in the other column i have the number of packets delivered in that seconds. 我有一张表,其中第一列中有表示秒数的整数(7、8、17、467等),而另一列中有该秒数中传递的数据包的数量。 I would like to sum all the packets that occurs every second in range of 10 seconds. 我想对所有每秒在10秒范围内发生的数据包求和。 So i would like to have the number of packets after every 10 seconds for example, in order to have a better visualization of the problem. 因此,例如,我想每隔10秒就有一个数据包的数量,以便更好地可视化问题。 A problem is that i don't have packets at each second but for example in the second number 5 i don't have packets and the row with the time=5 does not exist. 一个问题是我每秒都没有数据包,但是例如第二个数字5我没有数据包,并且time = 5的行不存在。

Anyone have some suggestions? 有人有建议吗?

rpl_dio = data.loc[data['MessageLabel'] == 0]
rpl_dio['Time'] = rpl_dio['Time'].astype(int)
rpl_dio_total = rpl_dio.groupby('Time')['MessageLabel'].count().reset_index(name='PackTime')
rpl_dio_total = rpl_dio_total.sort_values(by='Time',ascending=True)

plt.figure(figsize=(15,9))
plt.plot(rpl_dio_total['Time'],rpl_dio_total['PackTime'])
plt.title( "DIO packets rate" )
plt.ylabel( "Number of packets" )
plt.xlabel( "Time [s]" )
plt.show()

I would first add a new column with Timestamp (put your date in), and then combine it with a timedelta of the seconds 我首先要添加带有时间戳的新列(输入您的日期),然后将其与秒的时间增量合并

df['Seconds'] = pd.Timestamp('2019/01/01 00:00:00') + pd.to_timedelta(df['Time'], unit='s')

Out[61]: 
   Time  PackTime             Seconds
0     7        32 2019-01-01 00:00:07
1     9        53 2019-01-01 00:00:09
2    10        34 2019-01-01 00:00:10
3    11        53 2019-01-01 00:00:11
4    12        34 2019-01-01 00:00:12

and set the 'Seconds' column as your index 并将'Seconds'列设置为索引

df.set_index('Seconds', inplace=True)
Out[62]: 
                     Time  PackTime
Seconds                            
2019-01-01 00:00:07     7        32
2019-01-01 00:00:09     9        53
2019-01-01 00:00:10    10        34
2019-01-01 00:00:11    11        53
2019-01-01 00:00:12    12        34

now you can use the resample() method where '10S' is 10 seconds 现在您可以使用resample()方法,其中'10S'为10秒

df['PackTime'].resample('10S').sum()

 Out[63]: 
 Seconds
 2019-01-01 00:00:00     85
 2019-01-01 00:00:10    121
 Freq: 10S, Name: PackTime, dtype: int64

This is a small part of the dataset 这只是数据集的一小部分

在此处输入图片说明

请尝试以下方法:

pd.cut(df.Time, bins=np.arange(0, 100, 10)).groupby('Time').count()

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

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