我有以下数据帧:
df_h00 = df.copy()
tt = df_h00.set_index('username').post_time_data.str.extractall(r'totalCount\":([^,}]*)')
tt['index']=tt.index
tt[['user','hour']]=pd.DataFrame(tt['index'].values.tolist(),
index=tt.index)
tt = tt.drop(['index'], axis=1)
tt.columns = ['totalCount', 'user', 'hours']
tt.head()
totalCount user hours
username match
lowi 0 15 lowi 0
1 11 lowi 1
2 2 lowi 2
3 0 lowi 3
4 0 lowi 4
我想将tt['hours']列non-null int64
转换为格式为“%H:%M”的日期时间。
我尝试了以下代码:
tthour = tt['hours']
tthour = pd.to_datetime(tthour, format='%H', errors='coerce')
tthour = tthour.to_frame()
tthour.head()
hours
username match
lowi 0 1900-01-01 00:00:00
1 1900-01-01 01:00:00
2 1900-01-01 02:00:00
3 1900-01-01 03:00:00
4 1900-01-01 04:00:00
但是,我只想要“%H:%M”。所以预期的输出是这样的:
hours
username match
lowi 0 00:00
1 01:00
2 02:00
3 03:00
4 04:00
python中不存在所需格式的Datetimes
用^{} 或字符串^{} 关闭timedeltas:
相关问题 更多 >
编程相关推荐