'How to replace a row in pandas with multiple rows after applying a function?

I have a pandas dataframe that contains only one column which contains a string. I want to apply a function to each row that will split the string by sentence and replace that row with rows generated from the function.

Example dataframe:

import pandas as pd
df = pd.DataFrame(["A sentence. Another sentence. More sentences here.", "Another line of text"])

Output of df.head():

                                                   0
0  A sentence. Another sentence. More sentences h...
1                               Another line of text

I have tried using apply() method as follows:

def get_sentence(row):
    return pd.DataFrame(re.split('\.', row[0]))
df.apply(get_sentence, axis=1)

But then df.head() gives:

0                          0
0            A sentenc...
1                            0
0  Another line of text

I want the output as:

                     0
0            A sentence
1      Another sentence
2   More sentences here
3  Another line of text

What is the correct way to do this?



Solution 1:[1]

Convert all your strings to a 'flat' list, and build a new DataFrame or Series of that.

pd.DataFrame([item for sublist in list(df[0].str.split('.')) for item in sublist])

Be careful though. If you have elements that only consist of a '.', this will result in weird/blank new rows.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KingOtto