简体   繁体   English

如何使用熊猫的字符串索引将一列拆分为多列?

[英]How to split a column into multiple columns with the index of the string with pandas?

I have data frame, it looks like: 我有数据框,它看起来像:

df = pd.DataFrame({"a":["sea001", "seac002"]})
print(df)

         a
0   sea001
1  seac002

I want to split the a column into two columns, the first three characters in column "b", the rest in column "c" 我想将一列分为两列,列“ b”中的前三个字符,列“ c”中的其余字符

         a    b     c
0   sea001  sea   001
1  seac002  sea  c002

I want to use df.a.str.split(), but there is no option for me to separate the words after the index. 我想使用df.a.str.split(),但是我没有选择在索引后面分隔单词。 How can I do this cleverly? 我该如何巧妙地做到这一点?

You can use str with slicing semantics to do this: 您可以将str与切片语义一起使用来执行此操作:

In [102]:
df['b'], df['c'] = df['a'].str[:3], df['a'].str[3:]
df

Out[102]:
         a    b     c
0   sea001  sea   001
1  seac002  sea  c002

try .str.extract() method: 尝试.str.extract()方法:

In [104]: df[['b','c']] = df.a.str.extract(r'(.{3})(.*)', expand=True)

In [105]: df
Out[105]:
         a    b     c
0   sea001  sea   001
1  seac002  sea  c002

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

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