简体   繁体   中英

How to convert all timestamp values to a numeric value in a dataframe column Python Pandas

I have a pandas data frame which has int, float, timestamp datatypes.I would like to convert the timestamp values to a numeric value Something like 2018-08-20 18:57:07.797 to 20180820185707797 and store it as a numeric replacing the original timestamp column. How can I do this in Python

Timestamp is stored internally as int64 as the number of nanoseconds from 1970-01-01 (I think). You can get this number by:

df['time'] = pd.to_datetime(df['time']).astype(np.int64)

Or if you really want what the format you said, try:

df['time'] = pd.to_datetime(df['time']).dt.strftime('%Y%m%d%H%M%S%f').apply(lambda x: int(x[:-3]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM