Skip to content
Advertisement

Converting a dataframe with a line separator

I make a function that accepts a dataframe as input:

JavaScript

And returns a dataframe, where a certain delimiter number (in the example, it is 6) is the passed parameter:

JavaScript

Here’s what I got:

JavaScript

How can I simplify the function and make it more versatile? How do I make the function faster? Thanks.

Advertisement

Answer

You can do this concisely with np.split() and df.explode():

JavaScript

Explanation for np.split() and np.where()

We use np.where() to find the indexes of sep:

JavaScript

However, np.split() does the splitting after each index, which puts sep at the beginning of each split:

JavaScript

Instead, OP wants to split before each index to keep sep at the end of each split, so we shift the splitting indexes (1 +) and remove the last splitting index which won’t exist anymore ([:-1]):

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement