2

I'm playing with Whatsapp's history chat. I want to split the message column into two columns - time and message.

enter image description here

In order to split the two with the delimiter " - " I tried:

history['message'] = pd.DataFrame([line.split(" - ",1) for line in history['message']])

But history['message'] becomes the time only.

I don't understand why, because the line.split(" - ", 1) supposes to give a list of 2 elements at most.

1 Answer 1

4

I think you need str.split with expand=True for return DataFrame:

history = pd.DataFrame({'message':['a - b','c - d - r']})

history[['a','b']] = history['message'].str.split(' - ', n=1, expand=True)
print (history)
     message  a      b
0      a - b  a      b
1  c - d - r  c  d - r

If no NaNs use:

history[['a','b']] = pd.DataFrame([line.split(" - ", 1) for line in history['message']])

For me return error:

history['a'] = pd.DataFrame([line.split(" - ", 1) for line in history['message']])
print (history)

ValueError: Wrong number of items passed 2, placement implies 1

So if for you it working, try check separator, because it seems there is no split:

Sample:

history['a'] = history['message'].str.split('^', n=1, expand=True)
print (history)
     message          a
0      a - b      a - b
1  c - d - r  c - d - r
Sign up to request clarification or add additional context in comments.

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.