我的数据基本上是这样的:
19:05:09 86
19:10:09 86
19:15:09 85
19:20:09 84
..
18:55:10 165
19:00:10 164
19:05:10 163这是24小时的数据。我更改为列到日期时间来处理数据,但是这都是相同的一天,我需要排序数据。我是说,在00以后一定是另一天了。有办法这么做吗?
当然,我可以像这样分割数据:
data2[(data.times >= data['times'][0])] # first day
data2[(data2.times < data2['times'][0])] # and second day再加一天到第二天。
然而,数据包括几天和所有时间列的相同格式。我必须按照日期时间列的第一个值,一天一天地分割数据24小时版本(很明显,数据中不存在只有小时的数据)。做这件事最好的方法是什么?
发布于 2021-12-10 16:59:44
这里有一种方法
import io
data= '''times
19:05:09
19:10:09
23:55:09
00:00:09
19:05:09
19:10:09
23:55:09
00:00:09
19:05:09
19:10:09
23:55:09
00:00:09
'''
df = pd.read_csv(io.StringIO(data), sep=' \s+', engine='python')
df['count'] = df.loc[df['times'].str[0:5]=='00:00'].groupby(df['times'].str[0:5]=='00:00').cumcount() + 1
# Then back or forward fill as needed
df['count'] = df['count'].bfill()
df['count'].ffill().fillna(0) times count
0 19:05:09 1.000
1 19:10:09 1.000
2 23:55:09 1.000
3 00:00:09 1.000
4 19:05:09 2.000
5 19:10:09 2.000
6 23:55:09 2.000
7 00:00:09 2.000
8 19:05:09 3.000
9 19:10:09 3.000
10 23:55:09 3.000
11 00:00:09 3.000https://stackoverflow.com/questions/70294510
复制相似问题