Python 如何将熊猫中的秒数替换为零

Python 如何将熊猫中的秒数替换为零,python,pandas,Python,Pandas,我在熊猫中有以下数据帧 code time 1 003002 1 053003 1 060002 1 073001 1 073003 code time new_time 1 003002 00:30:00 1 053003 05:30:00 1 060002 06:00:00 1

我在熊猫中有以下数据帧

  code     time
  1        003002
  1        053003
  1        060002
  1        073001
  1        073003
  code     time        new_time
  1        003002      00:30:00
  1        053003      05:30:00
  1        060002      06:00:00
  1        073001      07:30:00 
  1        073003      07:30:00
我想在pandas中生成以下数据帧

  code     time
  1        003002
  1        053003
  1        060002
  1        073001
  1        073003
  code     time        new_time
  1        003002      00:30:00
  1        053003      05:30:00
  1        060002      06:00:00
  1        073001      07:30:00 
  1        073003      07:30:00
我用下面的代码来做

df['new_time'] = pd.to_datetime(df['time'] ,format='%H%M%S').dt.time
我如何在熊猫身上做到这一点?

使用:

或通过索引删除最后2个值,然后将格式更改为
%H%M

df['time'] = pd.to_datetime(df['time'].str[:-2], format='%H%M').dt.time

print (df)
  code      time
0    1  00:30:00
1    1  05:30:00
2    1  06:00:00
3    1  07:30:00
4    1  07:30:00