df['distance'].iloc[0]
output: '0.02790952'
type(df['distance'].iloc[0])
output: str
df.shape
(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.
.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)
.astype(float)
%%timeit
df[0].astype(float)
336 ms ± 2.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
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)
pd.Series
and dtype
argument%%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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.