如何利用pandas将时间戳格式化日期字符串呢? 例如:将 1357295797 转为 2013-01-04 10:36:37 #!/usr/bin/python # -*- coding: UTF-8 -*- """ @author: Roc-xb """ import pandas as pd if __name__ == '__main__' : items = [[ 135
如何利用pandas将时间戳格式化日期字符串呢?
例如:将 1357295797 转为 2013-01-04 10:36:37
#!/usr/bin/python# -*- coding: UTF-8 -*-
"""
@author: Roc-xb
"""
import pandas as pd
if __name__ == '__main__':
items = [[1357295797, 1367633260, 1368633260], [1357295797, 1367633260, 1368633260]]
df = pd.DataFrame(items, columns=['atime', 'btime', 'ctime'])
print(df)
# 同时保留日期和时间
df['atime'] = pd.to_datetime(df['atime'], unit='s')
# 只保留时间
df['btime'] = pd.to_datetime(df['btime'], unit='s').dt.time
# 只保留日期
df['ctime'] = pd.to_datetime(df['ctime'], unit='s').dt.date
print(df)
运行截图: