1

I have a dataframe for example:

ID      Description 

1       Long lasting glasses,Fire resistant,Polarizer

I want every description column just containing the maximum length of 10 characters, if it exceeds new columns should be formed. Example:

ID   Description   Description2   Description3  Description4   Description5

1    Long Lasti     ng glasses    ,Fire resi     stant,Pola    rizer
Ch3steR
  • 19,076
  • 4
  • 25
  • 52
Atom Store
  • 733
  • 1
  • 3
  • 21

1 Answers1

3

str.extractall + unstack

We can extract the all the occurrences of capturing group in the regex pattern, then unstack to reshape

df['Description'].str.extractall(r'(.{10}|.+$)')[0].unstack()

match           0           1           2           3      4
0      Long lasti  ng glasses  ,Fire resi  stant,Pola  rizer

Regex Details:

  • (.{10}|.+$) : First capturing group
    • .{10} : Match any character exactly 10 times (First alternative)
    • .+ : Match any character one or more times (Second alternative)

See the online regex demo

Shubham Sharma
  • 52,812
  • 6
  • 20
  • 45