我有一个充满1秒钟数据的熊猫数据框“10/23/2017 6:00”。每次在文件中出现60次,我想知道是否有一种简单/高效/智能的方法来为每行添加秒数,这样我就得到了“10/23/2017 6:00:00,10 / 23/2017 6:00:01 ......“
答案 0 :(得分:1)
首先转换列to_datetime
,然后添加由cumcount
和to_timedelta
创建的second
:
df['time'] = pd.to_datetime(df['time'])
df['time'] += pd.to_timedelta(df.groupby('time').cumcount(), unit='s')
样品:
df = pd.DataFrame({'time':['10/23/2017 6:00'] * 60})
df['time'] = pd.to_datetime(df['time'])
df['time'] += pd.to_timedelta(df.groupby('time').cumcount(), unit='s')
print (df.head(10))
time
0 2017-10-23 06:00:00
1 2017-10-23 06:00:01
2 2017-10-23 06:00:02
3 2017-10-23 06:00:03
4 2017-10-23 06:00:04
5 2017-10-23 06:00:05
6 2017-10-23 06:00:06
7 2017-10-23 06:00:07
8 2017-10-23 06:00:08
9 2017-10-23 06:00:09