Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 使用panda的慢循环_Performance_Pandas_Loops - Fatal编程技术网

Performance 使用panda的慢循环

Performance 使用panda的慢循环,performance,pandas,loops,Performance,Pandas,Loops,我试图用前一行的值替换序列中的每个nan值。数据如下所示: 16.5 楠 16.5 楠 楠 十六, 楠 这是我的密码: import pandas as pd import numpy as np df=pd.read_csv('test.csv') AskPrice=df.AskPrice for i, line in enumerate(AskPrice): if np.isnan(line): AskPrice[i]=AskPrice[i-1] print(AskPrice)

我试图用前一行的值替换序列中的每个nan值。数据如下所示:

16.5

16.5

十六,

这是我的密码:

import pandas as pd
import numpy as np

df=pd.read_csv('test.csv')
AskPrice=df.AskPrice
for i, line in enumerate(AskPrice):
if np.isnan(line):
    AskPrice[i]=AskPrice[i-1]
print(AskPrice)
我希望它是:

16.5

16.5

16.5

16.5

16.5

十六,

十六,

我得到了结果,但完成这项任务花了很长时间。有没有更快的办法?提前谢谢

怎么样

df.fillna(method='ffill')

也许
df.ffill()
好的,我会做一些调查!谢谢大家!@Bharath这完全是一样的看是的,我知道我的意思是你要加上well@xxyy您是要在所有数据帧上还是在给定列上执行此操作?可能是重复的