本文翻译自:Creating an empty Pandas DataFrame, then filling it?
I'm starting from the pandas DataFrame docs here: http://pandas.pydata.org/pandas-docs/stable/dsintro.html 我从这里的pandas DataFrame文档开始: http ://pandas.pydata.org/pandas-docs/stable/dsintro.html
I'd like to iteratively fill the DataFrame with values in a time series kind of calculation. 我想在时间序列类型的计算中用值迭代地填充DataFrame。 So basically, I'd like to initialize the DataFrame with columns A, B and timestamp rows, all 0 or all NaN. 所以基本上,我想用列A,B和时间戳记行(全为0或全部为NaN)初始化DataFrame。
I'd then add initial values and go over this data calculating the new row from the row before, say row[A][t] = row[A][t-1]+1
or so. 然后,我将添加初始值,并遍历此数据,从之前的行计算出新行,例如row[A][t] = row[A][t-1]+1
左右。
I'm currently using the code as below, but I feel it's kind of ugly and there must be a way to do this with a DataFrame directly, or just a better way in general. 我目前正在使用下面的代码,但是我觉得这很丑陋,必须有一种直接使用DataFrame进行此操作的方法,或者通常来说是一种更好的方法。 Note: I'm using Python 2.7. 注意:我正在使用Python 2.7。
import datetime as dt
import pandas as pd
import scipy as s
if __name__ == '__main__':
base = dt.datetime.today().date()
dates = [ base - dt.timedelta(days=x) for x in range(0,10) ]
dates.sort()
valdict = {}
symbols = ['A','B', 'C']
for symb in symbols:
valdict[symb] = pd.Series( s.zeros( len(dates)), dates )
for thedate in dates:
if thedate > dates[0]:
for symb in valdict:
valdict[symb][thedate] = 1+valdict[symb][thedate - dt.timedelta(days=1)]
print valdict
#1楼
参考:https://stackoom.com/question/vptg/创建一个空的Pandas-DataFrame-然后填充它
#2楼
Here's a couple of suggestions: 这里有一些建议:
Use date_range
for the index: 使用date_range
作为索引:
import datetime
import pandas as pd
import numpy as np
todays_date = datetime.datetime.now().date()
index = pd.date_range(todays_date-datetime.timedelta(10), periods=10, freq='D')
columns = ['A','B', 'C']
Note: we could create an empty DataFrame (wi