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:
JavaScript
x
6
1
item
2
100_5151
3
101_1205
4
102_8153
5
6
how can I achieve the following output?
JavaScript
1
6
1
item id group
2
100_5151 100_ 5151
3
101_1205 101_ 1205
4
102_8153 102_ 8153
5
6
Thanks in advance.
Advertisement
Answer
You can split with the _
as a separator and then add again the _
to the id string:
JavaScript
1
3
1
id, group =item.split("_")
2
id=id+"_"
3