1

I have a dataframe

         Description

0        Hi there
1        He is my family.
2        He studies in the United States.

I want to split this data frame in the case: If the description is longer than 10 characters, the rest of the characters should be in the next line.

Expected Output:

             Description
0            Hi there
1            He is my f
2            amily.
3            He studies
4            in the Uni
5            ted States
6            .
Atom Store
  • 961
  • 1
  • 11
  • 35

2 Answers2

1

Use custom lambda function in Series.apply and then Series.explode:

f =  lambda data: [data[x:x+10] for x in range(0, len(data), 10)]
s = df['Description'].apply(f).explode().reset_index(drop=True)
print (s)
0      Hi there
1    He is my f
2        amily.
3    He studies
4     in the Un
5    ited State
6            s.
Name: Description, dtype: object
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Hi jezrael, it would be great if you would help me on https://stackoverflow.com/questions/66741785/how-to-split-datas-from-columns-and-add-to-a-list-from-a-dataframe-also-repeat – Atom Store Mar 24 '21 at 05:47
0

One way using pandas.Series.str.findall:

df["Description"].str.findall(".{1,10}").explode()

Output:

0      Hi there
1    He is my f
1        amily.
2    He studies
2     in the Un
2    ited State
2            s.
Name: Description, dtype: object
Chris
  • 29,127
  • 3
  • 28
  • 51