我使用Python(Pandas)来操作高频数据。基本上,我需要填写空白单元格。在
如果该行为空,则该行将填入之前已存在的观察值。在
我的原始数据示例:
Time bid ask
15:00 . .
15:00 . .
15:02 76 .
15:02 . 77
15:03 . .
15:03 78 .
15:04 . .
15:05 . 80
15:05 . .
15:05 . .
需要转换为
^{pr2}$这是我的代码:
#Import
tan=pd.read_csv('sample.csv')
#From here fill the blank cells
first_line = True
mydata = []
with open(tan, 'rb') as f:
reader = csv.reader(f)
# loop through each row...
for row in reader:
this_row = row
# now do the blank-cell checking...
if first_line:
for colnos in range(len(this_row)):
if this_row[colnos] == '':
this_row[colnos] = 0
first_line = False
else:
for colnos in range(len(this_row)):
if this_row[colnos] == '':
this_row[colnos] = prev_row[colnos]
mydata.append( [this_row] )
prev_row = this_row
但是,代码不起作用。在
系统指示:
TypeError: coercing to Unicode: need string or buffer, DataFrame found
如果你能帮我解决这个问题,我真的很感激。谢谢。在
还有一种鲜为人知的
ffill
方法:使用
fillna()
属性。可以将方法指定为forward fill
,如下所示相关问题 更多 >
编程相关推荐