1

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'.

Fred
  • 417
  • 5
  • 16
  • maybe try apply? ``df.B.apply(lambda x: sliced(x, 2)``. apply is usually the way to go when using another library within Pandas – sammywemmy Jan 06 '21 at 11:12

2 Answers2

1

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  .*
anky
  • 74,114
  • 11
  • 41
  • 70
1

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  .*
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53