python - Pandas:更快地将字符串元组列表转换为数据帧?

标签 python string list pandas dataframe

我从一个文本字段中得到以下输入系列,其中包含作为字符串的地理坐标元组:

import pandas as pd

coords = pd.Series([
   '(29.65271977700047, -82.33086252299967)',
   '(29.652914019000434, -82.42682220199964)',
   '(29.65301114200048, -82.36455186899968)',
   '(29.642610841000476, -82.29853169599966)',
])

我想解析这些元组中的数字并得到以下结果 DataFrame:

         lat        lon
0  29.652720 -82.330863
1  29.652914 -82.426822
2  29.653011 -82.364552
3  29.642611 -82.298532

这是我想出的:

str_coords = coords.str[1:-1].str.split(', ')
latlon = str_coords.apply(pd.Series).astype(float)
latlon.columns = ['lat', 'lon']

我的问题:对.apply(pd.Series) 的调用会“永远”出现在真实列表中,该列表大约有 120 万个条目。有没有更快的方法?

最佳答案

另一种访问列表的第一个和第二个元素的方法也是通过 str:

In [174]: coords = pd.Series([
   .....:    '(29.65271977700047, -82.33086252299967)',
   .....:    '(29.652914019000434, -82.42682220199964)',
   .....:    '(29.65301114200048, -82.36455186899968)',
   .....:    '(29.642610841000476, -82.29853169599966)'])

In [175]: str_coords = coords.str[1:-1].str.split(', ')

In [176]: coords_df = pd.DataFrame({'lat': str_coords.str[0], 'lon': str_coords.str[1]})

In [177]: coords_df.astype(float).head()
Out[177]:
         lat        lon
0  29.652720 -82.330863
1  29.652914 -82.426822
2  29.653011 -82.364552
3  29.642611 -82.298532
4  29.652720 -82.330863

一些时间表明我的解决方案和@ajcr 的解决方案都比 apply(pd.Series) 方法快得多(并且两者之间的差异可以忽略不计):

In [197]: coords = pd.Series([
   .....:    '(29.65271977700047, -82.33086252299967)',
   .....:    '(29.652914019000434, -82.42682220199964)',
   .....:    '(29.65301114200048, -82.36455186899968)',
   .....:    '(29.642610841000476, -82.29853169599966)'])

In [198]: coords = pd.concat([coords]*1000, ignore_index=True)


In [199]: %%timeit
   .....: str_coords = coords.str[1:-1].str.split(', ')
   .....: df_coords = pd.DataFrame({'lat': str_coords.str[0], 'lon': str_coords.str[1]}, dtype=float)
   .....:
100 loops, best of 3: 14.1 ms per loop

In [200]: %%timeit
   .....: str_coords = coords.str[1:-1].str.split(', ')
   .....: df_coords = str_coords.apply(pd.Series).astype(float)
   .....:
1 loops, best of 3: 821 ms per loop

In [201]: %%timeit
   .....: df_coords = coords.str.extract(r'\((?P<lat>[\d\.]+),\s+(?P<lon>[^()\s,]+)\)')
   .....: df_coords.astype(float)
   .....:
100 loops, best of 3: 16.2 ms per loop

关于python - Pandas:更快地将字符串元组列表转换为数据帧?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28319952/

相关文章:

python - 流式传输大型 csv 文件的最快方法是什么?

c - 在 C 中回显客户端服务器 - 我有一个开放的服务器,它从不同的客户端获取字符串 - 我如何在新的迭代中替换旧的 char[]

javascript - 盒式 Assets 管理器为 javascript 包返回的空内容

css - 使用 CSS 为有序列表编号中的子项设置样式

python - python中列表的高效缩减

python - 如何从包含 "\n"的列表中删除部分字符串?

python - 如何在 python 中导入 Tensorflow 库?

python - 绘制和保存大量图形时如何加速 matplotlib?

python - 导入错误 : DLL load failed: The specified module could not be found for numpy

java - 没有获得线程程序的预期输出