我从这里的熊猫数据框文档开始:http://pandas.pydata.org/pandas-docs/stable/dsintro.html
我想用时间序列计算中的值迭代地填充数据框 . 所以基本上,我想初始化数据框,包括列A,B和时间戳行,全部为0或全部为NaN .
然后我会添加初始值并检查此数据,计算前一行中的新行,比如行[A] [t] =行[A] [t-1] 1左右 .
我目前正在使用下面的代码,但我觉得它有点难看,必须有一种方法可以直接使用数据框,或者只是更好的方式 . 注意:我使用的是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
4 回答
以下是一些建议:
使用date_range作为索引:
注意:我们可以通过编写简单的方法创建一个空的DataFrame(使用
NaN
)要对数据执行这些类型的计算,请使用numpy数组:
因此我们可以创建DataFrame:
If you simply want to create an empty data frame and fill it with some incoming data frames later, try this:
在这个例子中,我使用this pandas doc创建一个新的数据帧,然后使用append用来自oldDF的数据写入newDF .
Have a look at this
假设一个包含19行的数据帧
将A列保持为常数
将列b保持为循环给出的变量
您可以用任何值替换pd.Series中的第一个x([x],index = [x])
If you want to have your column names in place from the start, use this approach:
If you want to add a record to the dataframe it would be better to use:
您可能还想传递字典:
However if you want to add another dataframe to my_df do as follows:
if you are adding rows inside a loop consider performance issues;
对于大约前1000个记录"my_df.loc"性能更好,并且通过增加循环中的记录数量逐渐变慢 .
If you plan to do thins inside a big loop (say 10M records or so)
你最好混合使用这两种;使用iloc填充数据帧,直到大小达到1000,然后将其附加到原始数据帧,并清空临时数据帧 . 这会使你的表现提高10倍左右