Home > Software engineering >  Capitalize text between parentheses in pandas column
Capitalize text between parentheses in pandas column

Time:01-17

I have a pandas column that contains a list of text:

["apple", "orange (fruit)", "banana"]

I would like to capitalize the text between the parentheses of the text so that the final output would be:

["apple", "orange (FRUIT)", "banana"]

CodePudding user response:

You can use Series.str.replace with a regular expression:

col.str.replace(r"\(. \)", lambda x: x.group(0).upper(), regex=True)

This outputs:

0             apple
1    orange (FRUIT)
2            banana
  • Related