1

Is there a way to split a dataframe on the 4th delimiter from the right?

For example, if I wanted to split this:

A
Hello, world, what can, I do for, you
Hi, Hi, Hi, Hi, Hi, Hi
Yes, Yes, Heloo, Hi, Hi, Hi, Hi

Into this:

A                                        B                      C
Hello, world, what can, I do for, you    Hello                  world, what can, I do for, you
Hi, Hi, Hi, Hi, Hi, Hi                   Hi, Hi                 Hi, Hi, Hi, Hi
Yes, Yes, Heloo, Hi, Hi, Hi, Hi          Yes, Yes, Heloo        Hi, Hi, Hi, Hi

I know that I can use the following to split on the last delimiter.

NEWDF = DF.A.str.rsplit(', ', 1, expand=True).rename(lambda x: f'col{x + 1}', axis=1)

I am just wondering if there is a way to split on the x from last...

0

1 Answer 1

2

Try this, it should work.

delimiter = ', '
A = 'Hello, world, what can, I do for, you'
splitted = A.split(delimiter)
B = delimiter.join(splitted[:-4])
C = delimiter.join(splitted[-4:])

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.