python里数据框拆分,在Python中将数据框拆分为多个5秒的数据框

I have a relatively big dataset that I want to split into multiple dataframes in Python based on a column containing a datetime object. The values in the column (that I want to split the dataframe by) are given in the following format:

2015-11-01 00:00:05

How can I split the dataframe into 5-second intervals in the following way:

1st dataframe 2015-11-01 00:00:00 - 2015-11-01 00:00:05,

2nd dataframe 2015-11-01 00:00:05 - 2015-11-01 00:00:10, and so on.

I also need to count the number of observations in each of resulting dataframes. In other, words, it would be nice if I could get another dataframe with 2 columns: 1st representing the splitted group (values of this column don't matter: they could be simply 1, 2, 3,.. indicating the order of the 5-second intervals ), 2nd column showing the number of observations belonging to the respective intervals

解决方案

I think the best for store multiple DataFrames is dict:

rng = pd.date_range('2015-11-01 00:00:00', periods=100, freq='S')

df = pd.DataFrame({'Date': rng, 'a': range(100)})

print (df.head(10))

Date a

0 2015-11-01 00:00:00 0

1 2015-11-01 00:00:01 1

2 2015-11-01 00:00:02 2

3 2015-11-01 00:00:03 3

4 2015-11-01 00:00:04 4

5 2015-11-01 00:00:05 5

6 2015-11-01 00:00:06 6

7 2015-11-01 00:00:07 7

8 2015-11-01 00:00:08 8

9 2015-11-01 00:00:09 9

dfs={k.strftime('%Y-%m-%d %H:%M:%S'):v for k,v in

df.groupby(pd.Grouper(key='Date', freq='5S'))}

print (dfs['2015-11-01 00:00:00'])

Date a

0 2015-11-01 00:00:00 0

1 2015-11-01 00:00:01 1

2 2015-11-01 00:00:02 2

3 2015-11-01 00:00:03 3

4 2015-11-01 00:00:04 4

print (dfs['2015-11-01 00:00:05'])

Date a

5 2015-11-01 00:00:05 5

6 2015-11-01 00:00:06 6

7 2015-11-01 00:00:07 7

8 2015-11-01 00:00:08 8

9 2015-11-01 00:00:09 9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值