给定一个Series带有字符串的 Pandas ,我想DataFrame为Series基于位置的每个部分创建一个列。例如,给定以下输入:s = pd.Series(['abcdef', '123456'])ind = [2, 3, 1]理想情况下,我会得到这个:target_df = pd.DataFrame({ 'col1': ['ab', '12'], 'col2': ['cde', '345'], 'col3': ['f', '6']})一种方法是一一创建它们,例如:df['col1'] = s.str[:3]df['col2'] = s.str[3:5]df['col3'] = s.str[5]但我猜这比单次拆分要慢。我尝试了正则表达式,但不确定如何解析结果:pd.DataFrame(s.str.split("(^(\w{2})(\w{3})(\w{1}))"))# 0# 0 [, abcdef, ab, cde, f, ]# 1 [, 123456, 12, 345, 6, ]
1 回答

人到中年有点甜
TA贡献1895条经验 获得超7个赞
您的正则表达式几乎就在那里(注意Series.str.extract(expand=True)返回 a DataFrame):
df = s.str.extract("^(\w{2})(\w{3})(\w{1})", expand = True)
df.columns = ['col1', 'col2', 'col3']
# col1 col2 col3
# 0 ab cde f
# 1 12 345 6
这是一个概括这一点的函数:
def split_series_by_position(s, ind, cols):
# Construct regex.
regex = "^(\w{" + "})(\w{".join(map(str, ind)) + "})"
df = s.str.extract(regex, expand=True)
df.columns = cols
return df
# Example which will produce the result above.
split_series_by_position(s, ind, ['col1', 'col2', 'col3'])
添加回答
举报
0/150
提交
取消