Reputation: 80
I have a dataframe like this:
df1 = pd.DataFrame({'Parent': ['Stay home', "Stay home","Stay home", 'Go outside'],
'Child' : ['Severe weather', "Severe weather", "Severe weather", 'Sunny'],
'Score': ['(Score: 0.0310)', '(Score: 0.0310)', '(Score: 0.0310)', '(Score: 0.0310)']})
Parent Child Score
0 Stay home Severe weather (Score: 0.0310)
1 Stay home Severe weather (Score: 0.0310)
2 Stay home Severe weather (Score: 0.0310)
3 Go outside Sunny (Score: 0.0310)
I want to delete the parenthesis and score:
from the score column:
Parent Child Score
0 Stay home Severe weather 0.0310
1 Stay home Severe weather 0.0310
2 Stay home Severe weather 0.0310
3 Go outside Sunny 0.0310
Any ideas?
Upvotes: 0
Views: 105
Reputation: 25313
Another possible solution, based on pandas.Series.str.strip
:
df1['Score'] = df1['Score'].str.strip('\(Score: |\)')
Output:
Parent Child Score
0 Stay home Severe weather 0.0310
1 Stay home Severe weather 0.0310
2 Stay home Severe weather 0.0310
3 Go outside Sunny 0.0310
Upvotes: 0
Reputation: 103
Can use re and findall
import re
df1['Score'] = df1['Score'].apply(lambda x: re.findall('Score: (.*?)\)', x)[0])
>>> df1
Parent Child Score
0 Stay home Severe weather 0.0310
1 Stay home Severe weather 0.0310
2 Stay home Severe weather 0.0310
3 Go outside Sunny 0.0310
Upvotes: 0