if i have a value like this :
C:100% B:90% A:80%
i want to add comma after every % so the output is like this :
C:100%,B:90%,A:80%
i've tried somthing like :
data['Final'] = data['Final'].str.replace(r'(%)\n\b', r'\1,', regex=True)
CodePudding user response:
You can use the re.sub
method from the re
module in Python to achieve this.
import re
# Your original string
string = "C:100% B:90% A:80%"
# Use regex to replace all occurrences of '%' with ',%'
string = re.sub("%", ",%", string)
# The resulting string will be: "C:100%, B:90%, A:80%"
If you want to apply this to a column in a DataFrame, you can use the apply method to apply the regex substitution to each value in the column. For example:
import pandas as pd
import re
# Create a DataFrame with a column of strings
df = pd.DataFrame({"values": ["C:100% B:90% A:80%", "D:70% E:60% F:50%"]})
# Use the apply method to apply the regex substitution to each value in the column
df["values"] = df["values"].apply(lambda x: re.sub("% ", "%,", x))
This will result in a DataFrame with the following values in the values column:
0 C:100%,B:90%,A:80%
1 D:70%,E:60%,F:50%
CodePudding user response:
You can use this :
df['final']= df['final'].str.replace(r'%\s*\b', r'%,', regex=True)
Output :
print(df)
final
0 C:100%,B:90%,A:80%
CodePudding user response:
There is no newline in your example data, so you could write the pattern matching just a space, or 1 or more whitespace chars \s
data = pd.DataFrame({"Final": ["C:100% B:90% A:80%"]})
data['Final'] = data['Final'].str.replace(r'(%) \b', r'\1,', regex=True)
print(data)
Output
Final
0 C:100%,B:90%,A:80%