我有一个数据框
import pandas as pd
import datetime
#Create data
df = {'Day': ['2012-12-29','2013-01-05'],
'Whatev': [1,0]}
index = range(len(df['Day']))
columns = ['Day','Whatev']
df = pd.DataFrame(df, index=index, columns=columns)
print(df)
print()
从中提取第一列为一系列:
#Index to datetime
df['Day'] = pd.to_datetime(df['Day'])
df.set_index('Day', inplace=True) #use day as index
#Create series from dataframe column
s = df[df.columns[0]]
print(s)
print()
我想在末尾附加零值。因此,我首先扩展datetime索引,然后将最后一个值指定为零:
#Append 1 data to series
last_date = s.iloc[[-1]].index + datetime.timedelta(days=7)
s.append(pd.DataFrame(index=[last_date]))
s.iloc[[-1]]=0
print(s)
print()
是的,我没有工作。在测试期间,它似乎可以正常工作,但是在某种程度上,这里的数据框保持不变。是什么原因?
还有,有一种更聪明的方法来达到目的吗?实际上,如果我想附加多个值,我的解决方案看起来会很糟糕:
#Append many data to series
L = 2 #hoe many zeros
i=0
while i<=L:
i+=1
last_date = s.iloc[[-1]].index + datetime.timedelta(days=7)
s.append(pd.DataFrame(index=[last_date]))
s.iloc[[-1]]=0
print(s)
答案 0 :(得分:2)
您可以使用setting-with-enlargement
简化代码:
sorted_lines
循环版本:
df['Day'] = pd.to_datetime(df['Day'])
s = df.set_index('Day')['Whatev']
非循环版本:
L = 2 #hoe many zeros
i=0
while i<=L:
i+=1
last_date = s.index[-1] + pd.Timedelta(days=7)
s.loc[last_date]=0
print(s)
2012-12-29 1
2013-01-05 0
2013-01-12 0
2013-01-19 0
2013-01-26 0
Name: Whatev, dtype: int64