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            .

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Hi jezrael, it would be great if you would help me on stackoverflow.com/questions/66741785/…
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.