Pandas 在数据框中追加日期
我的数据框架如下:Pandas 在数据框中追加日期,pandas,Pandas,我的数据框架如下: Dates 0 2001-01-01 1 2001-01-02 2 2001-01-03 3 2001-01-04 我想附加两个连续的日期并获得以下数据帧 Dates 0 2001-01-01 1 2001-01-02 2 2001-01-03 3 2001-01-04 4 2001-01-05 5 2001-01-06 new = pd.date
Dates
0 2001-01-01
1 2001-01-02
2 2001-01-03
3 2001-01-04
我想附加两个连续的日期并获得以下数据帧
Dates
0 2001-01-01
1 2001-01-02
2 2001-01-03
3 2001-01-04
4 2001-01-05
5 2001-01-06
new = pd.date_range(dates.Dates.iloc[-1], periods=2)
print (new)
使用,但需要通过索引删除第一个新值[1://code>:
new = pd.date_range(df.Dates.iloc[-1], periods=3)
s = pd.Series(new[1:])
new = df['Dates'].append(s, ignore_index=True)
print (new)
0 2001-01-01
1 2001-01-02
2 2001-01-03
3 2001-01-04
4 2001-01-05
5 2001-01-06
dtype: datetime64[ns]
或重新构造数据帧
:
new = pd.date_range(df.Dates.iloc[0], periods=len(df) + 2)
df1 = pd.DataFrame(new, columns=['Dates'])
print (df1)
Dates
0 2001-01-01
1 2001-01-02
2 2001-01-03
3 2001-01-04
4 2001-01-05
5 2001-01-06
我还想将索引0附加到5@ian只需执行
new.reset_index(drop=True,inplace=True)
@shivsn attributeerError:'DatetimeIndex'对象没有属性'reset_index'@ian-不需要,忽略_index=True
帮助它。
new = pd.date_range(df.Dates.iloc[0], periods=len(df) + 2)
df1 = pd.DataFrame(new, columns=['Dates'])
print (df1)
Dates
0 2001-01-01
1 2001-01-02
2 2001-01-03
3 2001-01-04
4 2001-01-05
5 2001-01-06