Python-根据时间戳记值为组中的每个唯一值分配唯一ID

时间:2018-11-08 18:58:59

标签: python pandas

我遇到的问题与此帖子https://stackoverflow.com/questions/45086369/assign-unique-id-to-each-unique-value-in-group-after-pandas-groupby类似,但是,groupby的其中一列是Timestamp,它给我的输出很奇怪,如下所示:

from pandas import Timestamp
df=pd.DataFrame({'day': [Timestamp('2017-03-27'),
                         Timestamp('2017-03-27'),Timestamp('2017-04-01'),
                         Timestamp('2017-04-03'),Timestamp('2017-04-06'),
                         Timestamp('2017-04-07'),Timestamp('2017-04-11'),
                         Timestamp('2017-05-01'),Timestamp('2017-05-01')],
'act_id': ['916298883','916298883','916298883','926539428','930641527',
           '930641527','930641527','966163233','966163233']})

我这样做了:

df['no_adm'] = df.groupby('act_id')['day'].transform(lambda x: pd.factorize(x)[0]+1)

它给了我

enter image description here

我不知道1970-01-01 00:00:00.0000000000是怎么来的并将其删除。我只希望我的数据框在no_adm列中不包含那些timestamp元素。

0 个答案:

没有答案