如何使用日期时间格式转换具有时代的df中的列,反之亦然?

问题描述 投票:-1回答:1

1.具有价值观的df

    0            1 
0 sun       | 1549628346168
1 goal      | 1532230566808
2 micheal   | 1533647352218

输出格式应为

    0            1 
0 sun       | 2019-02-02 Sat 21:16:20
1 goal      | 2019-02-02 Sat 21:16:20
2 micheal   | 2019-02-02 Sat 21:16:20

试过这个如何用言语来表达

s = time/1000
datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S')

2.

有df值如何连接这两个并得到一个具有纪元时间的列

    Date           Time 
0  08-09-2017 |  05:00 PM
1  16-06-17   |  10:27 AM
2   04-07-17  |  03:11 PM

产量

    time
 1549628346168
 1532230566808
 1533647352218
python python-3.x pandas dataframe epoch
1个回答
3
投票

首先使用to_datetimeunit='ms',然后使用Series.dt.strftime添加%a几天,同时检查http://strftime.org/

df[1] = pd.to_datetime(df[1], unit='ms').dt.strftime('%Y-%m-%d %a %H:%M:%S')

print (df)
         0                        1
0      sun  2019-02-08 Fri 12:19:06
1     goal  2018-07-22 Sun 03:36:06
2  micheal  2018-08-07 Tue 13:09:12

编辑:

df['dt'] = pd.to_datetime(df['Date'] + ' ' + df['Time']).astype(np.int64) // 10**9
print (df)
         Date      Time          dt
0  08-09-2017  05:00 PM  1502298000
1    16-06-17  10:27 AM  1497608820
2    04-07-17  03:11 PM  1491577860
© www.soinside.com 2019 - 2024. All rights reserved.