Steven01123581321
Steven01123581321

Reputation: 460

How to split AFTER underscore in Python

I've seen a lot of threads that say how to split based on an underscore, but how can we split a string where the split is done after the underscore.

So let's say I have a pandas dataframe with one column:

 item
100_5151
101_1205
102_8153
...

how can I achieve the following output?

  item      id    group
100_5151   100_   5151 
101_1205   101_   1205 
102_8153   102_   8153
...

Thanks in advance.

Upvotes: 0

Views: 329

Answers (1)

jbm04
jbm04

Reputation: 56

You can split with the _ as a separator and then add again the _ to the id string:

id, group =item.split("_")
id=id+"_"

Upvotes: 4

Related Questions