简体   繁体   English

使用Python中的Dataframe索引创建包含数据的列表

[英]Creating a list with data from a Dataframe index in Python

i'm facing troubles trying to convert the index of df (see code) into a list, because when i run it this list is a "Timestamp" one (i dont know what that means). 我在尝试将df的索引(请参见代码)转换为列表时遇到了麻烦,因为当我运行它时,该列表是一个“时间戳”(我不知道那是什么意思)。 The code is 该代码是

quote='AAPL'
stock = DataReader(quote,'yahoo',start_date,end_date)
stock.head()
df=DataFrame(stock)
xdata = df.index.tolist()

And then when run i get 然后当我跑

[Timestamp('2010-01-04 00:00:00'), Timestamp('2010-01-05 00:00:00'), Timestamp('2010-01-06 00:00:00'), Timestamp('2010-01-07 00:00:00'), Timestamp('2010-01-08 00:00:00')]

But i want to have something like this: 但是我想要这样的东西:

['2010-01-04', '2010-01-05'), ...,'2010-01-08')]

Can somebody please help me out with this? 有人可以帮我这个忙吗?

Thanks in advance! 提前致谢!

ts_list = df.index.tolist()  # a list of Timestamp's
date_list = [ ts.date() for ts in ts_list ]  # a list of datetime.date's
date_str_list = [ str(date) for date in date_list ]  # a list of strings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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