![](/img/trans.png)
[英]Create new row in data-frame if date exists in a list and not in the data-frame
[英]Copy data-frame date index into a list
我有一个头部的数据框df
看起来像:
amount_1 ... factor
date ...
2014-04-21 931.550533 ... 100.0
2014-04-22 932.174195 ... 100.0
2014-04-23 946.622481 ... 100.0
2014-04-24 938.722707 ... 100.0
2014-04-25 949.325041 ... 100.0
我想获取索引( date
)并创建一个列表。 我在用:
dates = df.index.map(pd.Timestamp.date).tolist()
这给了我一个输出:
[datetime.date(2014, 2, 21), datetime.date(2014, 2, 22), datetime.date(2014, 2, 23), datetime.date(2014, 2, 24), datetime.date(2014, 2, 25), .....]
但是我希望输出看起来像:
[2014-04-21,2014-04-22,2014-04-23,2014-04-24,2014-04-25,....]
我怎样才能做到这一点?
如果想要字符串中的YYYY-MM-DDDD
使用DatetimeIndex.strftime
:
dates = df.index.strftime('%Y-%m-Yd')
如果想要删除时间,这意味着设置为00:00:00
使用DatetimeIndex.floor
:
dates = df.index.floor('d')
您的解决方案应该使用DatetimeIndex.date
更改:
dates = df.index.date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.