【发布时间】:2014-04-05 03:59:36
【问题描述】:
我有一个 pandas 数据框,其中有一个字符串列。帧的长度超过 200 万行,循环提取我需要的元素是一个糟糕的选择。我当前的代码如下所示
for i in range(len(table["series_id"])):
table["state_code"] = table["series_id"][i][2:4]
table["area_code"] = table["series_id"][i][5:9]
table["supersector_code"] = table["series_id"][i][11:12]
其中“series_id”是包含多个信息字段的字符串我要创建一个示例数据元素:
列:
[series_id, year, month, value, footnotes]
数据:
[['SMS01000000000000001' '2006' 'M01' 1966.5 '']
['SMS01000000000000001' '2006' 'M02' 1970.4 '']
['SMS01000000000000001' '2006' 'M03' 1976.6 '']
但是 series_id 是我正在努力解决的感兴趣的列。我已经查看了 python 的 str.FUNCTION,特别是 pandas。
有一个部分描述了每个字符串函数,即特别是 get 和 slice 是我想使用的函数。理想情况下,我可以设想这样的解决方案:
table["state_code"] = table["series_id"].str.get(1:3)
或
table["state_code"] = table["series_id"].str.slice(1:3)
或
table["state_code"] = table["series_id"].str.slice([1:3])
当我尝试以下函数时,我得到“:”的无效语法。
但是我似乎无法找出正确的方法来执行向量操作以在熊猫数据框列上获取子字符串。
谢谢
【问题讨论】:
-
我想你想要的是
table["state_code"] = table["series_id"].str[1:3] -
注意:这是一种非常糟糕的遍历行的方法,要么使用 iterrows,要么使用 apply。使用 range 这样创建一个巨大的 python 列表(在 python 2 中),xrange 稍微好一点。
标签: python string pandas substring