[英]Convert timestamps from a dataframe column to date format
The following data frame is the result of以下数据框是
top5 = df.nlargest(5,columns='B',keep='all')
A B C D E F G H I
77 2013-08-17 42302 2278503 100644.38 5021507.24 72471 5729 5729 217
70 2013-08-10 42245 2016000 103374.87 4492859.82 69996 5567 5567 223
74 2013-08-14 42226 2154625 78504.31 4766845.59 71481 1727 1727 219
66 2013-08-06 42086 1871575 81608.23 4209532.51 68744 1778 1778 222
59 2013-07-30 42032 1611459 79312.12 3691785.45 65855 1701 1701 237
I would like to print only dates as my result.我想只打印日期作为结果。 I've tried something like this to print only the
A
column:我已经尝试过这样的事情来只打印
A
列:
print(top5['A'].values.tolist())
But it gives me this result:但它给了我这个结果:
[1376697600000000000, 1376092800000000000, 1376438400000000000, 1375747200000000000, 1375142400000000000]
But I need a result like:但我需要这样的结果:
[2013-08-17,2013-08-10,2013-08-14,2013-08-06,2013-07-30]
What's the way to convert this timestamp to date format?将此时间戳转换为日期格式的方法是什么?
Enclose the column in list().将列括在 list() 中。
print(list(top5['A'].astype('datetime64[ns]')))
to get a list of strings from a datetime column, use要从日期时间列中获取字符串列表,请使用
import pandas as pd
# what you have is like
top5 = pd.DataFrame({'A': pd.to_datetime(['2013-08-17', '2013-08-10', '2013-08-14'])})
# note the column A is of dtype datetime
# format to string using strftime and then cast to_list():
l_A = top5['A'].dt.strftime('%Y-%m-%d').to_list()
# l_A
# ['2013-08-17', '2013-08-10', '2013-08-14']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.