I have the following dataframe as an example:
test = pd.DataFrame({'type':['fruit-of the-loom (sometimes-never)', 'yes', 'ok (not-possible) I will try', 'vegetable', 'poultry', 'poultry'],
'item':['apple', 'orange', 'spinach', 'potato', 'chicken', 'turkey']})
I found many posts of people wanting to remove parentheses from strings or similar situations, but in my case I would like to retain the string exactly as is, except I would like to remove the hyphen that is inside the parenthesis of the string.
Does anyone have a suggestion on how I could achieve this?
str.split() would take care of the hyphen if it was leading and str.rsplit() if it was trailing. I can't think of a way to engage this.
in this case the ideal outcome for the values in this hypothetical column would be:
'fruit-of the-loom (sometimes never)',
'yes',
'ok (not possible) I will try',
'vegetable',
'poultry',
'poultry'`
One way could be to use str.replace with a pattern looking for what is between parenthesis, and the replace parameter could be a lambda using replace on the matching object:
print (test['type'].str.replace(pat='\((.*?)\)',
repl=lambda x: x.group(0).replace('-',' ')))
0 fruit-of the-loom (sometimes never)
1 yes
2 ok (not possible) I will try
3 vegetable
4 poultry
5 poultry
Name: type, dtype: object
Explanation of what is in pat= can be found here
test.type = (test.type.str.extract('(.*?\(.*?)-(.*?\))(.*)')
.sum(1)
.combine_first(test.type))
Explanation:
beginning until parenthesis and then hyphen and after hyphen until parenthesis and then optional additional stuffsumNaN, use the values from the original (combine_first)This way the hyphen is dropped, not replaced by a space. If you need a space you could use apply instead of sum:
test.type = (test.type.str.extract('(.*?\(.*?)-(.*?\))(.*)')
.apply(lambda row: ' '.join(row.values.astype(str)), axis=1)
.combine_first(test.type))
Either way, this won't work for more than one set of parentheses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With