Fred
Fred

Reputation: 583

panda series object: cut string into equal chunks

I found a solution to cut a string like s = '247451517328' here: Split string every nth character? using using more_itertools sliced. But how can I apply this to a dataframe elegantly? e. g. a dataframe df

A    B
1    *N8111111.
2    BC1111111*
3    77N01101.*

should become (sliced by every second character)

A    B  C  D  E  F
1    *N 81 11 11 1.
2    BC 11 11 11 1*
3    77 N0 11 01 .*

I tried

from more_itertools import sliced
df.str.sliced('1234567890', 2)

but this doesn't work since str has no method 'sliced'.

Upvotes: 1

Views: 59

Answers (2)

anky
anky

Reputation: 75080

You solution will work with apply since slice takes a single string not a series:

from more_itertools import sliced
df[['A']].join(pd.DataFrame(df['B'].apply(lambda x: sliced(x, 2)).tolist()))

   A   0   1   2   3   4
0  1  *N  81  11  11  1.
1  2  BC  11  11  11  1*
2  3  77  N0  11  01  .*

Upvotes: 1

Shubham Sharma
Shubham Sharma

Reputation: 71689

You can use str.findall:

df[['A']].join(pd.DataFrame(df['B'].str.findall(r'.{2}|.{1}').tolist()))

   A   0   1   2   3   4
0  1  *N  81  11  11  1.
1  2  BC  11  11  11  1*
2  3  77  N0  11  01  .*

Upvotes: 1

Related Questions