Pandas DataFrame按条件筛选数据后索引不连续问题的处理

Pandas DataFrame按条件筛选数据后索引不连续问题的处理

方法一:直接引用不连续的索引

例如:

#df.index就是取当前索引,与索引是否连续无关
df['starttime_datetime']=[datetime.datetime.fromtimestamp(time.mktime(df['Start_Time_Format'][i])) for i in df.index]

方法二:重建索引

tmp=df['endtime-starttime'][0:10]
print(tmp)
print(tmp.shape)
0    00:05:21
5    00:09:46
18   00:07:14
21   00:11:53
23   00:15:17
25   00:25:59
27   00:03:35
31   00:43:30
32   00:16:18
33   00:06:50
Name: endtime-starttime, dtype: timedelta64[ns]
(10,)
#转换为DataFrame,便于下一步增加一列。否则为list类型无法增加列
tmp=pd.DataFrame(tmp)  
print(tmp)
print(tmp.shape)
print(type(tmp))
   endtime-starttime
0           00:05:21
5           00:09:46
18          00:07:14
21          00:11:53
23          00:15:17
25          00:25:59
27          00:03:35
31          00:43:30
32          00:16:18
33          00:06:50
(10, 1)
<class 'pandas.core.frame.DataFrame'>
#给tmp增加一列
tmp['index']=range(tmp.shape[0])
print(tmp)
   endtime-starttime  index
0           00:05:21      0
5           00:09:46      1
18          00:07:14      2
21          00:11:53      3
23          00:15:17      4
25          00:25:59      5
27          00:03:35      6
31          00:43:30      7
32          00:16:18      8
33          00:06:50      9
#用set_index()方法将index列设置为索引
tmp.set_index('index')
print(tmp)
   endtime-starttime  index
0           00:05:21      0
5           00:09:46      1
18          00:07:14      2
21          00:11:53      3
23          00:15:17      4
25          00:25:59      5
27          00:03:35      6
31          00:43:30      7
32          00:16:18      8
33          00:06:50      9

参考文献:https://blog.csdn.net/htbeker/article/details/79417959

你可能感兴趣的:(Pandas DataFrame按条件筛选数据后索引不连续问题的处理)