简体   繁体   中英

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"

         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. How can I do this cleverly?

You can use str with slicing semantics to do this:

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:

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

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.

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