-2

I have a df with one of the columns that appears like:

**Share**
We are safe 25%
We are always safe 12.50% (India Aus, West)
We are ok (USA, EU)
We are not OK
What is this
Always wise 25.66%

I want to split this column such that the % values wherever applicable get split from the column into a new one. So the output would be

Share                  Percent    LOCATION
We are safe            25%  
We are always safe     12.50%     India Aus, West
We are ok                         USA, EU
We are not OK
What is this
Always wise            25.66%
asimo
  • 2,340
  • 11
  • 29
  • 1
    Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Oct 14 '20 at 15:06

1 Answers1

0

Just base on your sample data:

print (df["Share"].str.extract('([A-Za-z\s]+)\s?(\d+[.0-9]+%)?\s?\(?(.*(?=\)))?'))

                     0       1                2
0         We are safe      25%              NaN
1  We are always safe   12.50%  India Aus, West
2           We are ok      NaN          USA, EU
3        We are not OK     NaN              NaN
4         What is this     NaN              NaN
5         Always wise   25.66%              NaN

Try it online here.

Henry Yik
  • 22,275
  • 4
  • 18
  • 40