简体   繁体   English

如何提高计算速度str到float

[英]how to improve the calculation speed str to float

df['distance'].iloc[0]

output: '0.02790952' output:'0.02790952'

type(df['distance'].iloc[0])

output: str output:str

df.shape

(118884, 40) (118884, 40)

I try to parse a string to a float我尝试将字符串解析为浮点数

for i in tqdm(range(len(df['distance']))):
    df['distance'].iloc[i] = float(df['distance'].iloc[i])

Here is a quick comparison between several methods.这是几种方法之间的快速比较。 I used a single column DataFrame, with 1 million rows.我使用了单列 DataFrame,有 100 万行。

Using .apply(np.float)使用.apply(np.float)

%%timeit 
df[0].apply(np.float)
476 ms ± 6.65 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Using .astype(float)使用.astype(float)

%%timeit
df[0].astype(float)
336 ms ± 2.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Using pd.to_numeric使用pd.to_numeric

%%timeit
pd.to_numeric(df[0])
244 ms ± 2.28 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Using pd.Series and dtype argument使用pd.Seriesdtype参数

%%timeit
pd.Series(df[0], dtype=np.float64)
333 ms ± 2.88 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

The winner is pd.to_numeric , To anyone who reads this, if you think of a faster way, please comment!获胜者是pd.to_numeric ,对于任何阅读此内容的人,如果您想到更快的方法,请发表评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM