gpt4 book ai didi

Pandas DatetimeIndex 到数据帧

转载 作者:行者123 更新时间:2023-12-03 08:59:55 24 4
gpt4 key购买 nike

如何将 DatetimeIndex 更改为像这样的简单数据框:

        month
0 2013-07-31
1 2013-08-31
2 2013-09-30
3 2013-10-31

这是日期时间索引:

DatetimeIndex(['2013-07-31', '2013-08-31', '2013-09-30', '2013-10-31',
'2013-11-30', '2013-12-31', '2014-01-31', '2014-02-28',
'2014-03-31', '2014-04-30', '2014-05-31', '2014-06-30'],
dtype='datetime64[ns]', freq='M')

谢谢。

最佳答案

下面的代码应该可以工作:

# Import libraries
import pandas as pd
import numpy as np
import datetime as dt

# Create dataframe
df = pd.DataFrame(pd.DatetimeIndex(['2013-07-31', '2013-08-31', '2013-09-30', '2013-10-31',
'2013-11-30', '2013-12-31', '2014-01-31', '2014-02-28',
'2014-03-31', '2014-04-30', '2014-05-31', '2014-06-30'],
dtype='datetime64[ns]', freq='M'), columns=['month'])
df.head(2)

enter image description here

关于Pandas DatetimeIndex 到数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51373398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com