I have a DataFrame where I would like to find a specific string and then replace this string with a different string.
Data
ID Date Stat
AA101.aa Q3 2022 AA101
AA101.aa Q3 2022 aa
AA Q4 2022 aa
BB Q4 2022 bb
Desired
ID Date Stat
AA001.aa Q3 2022 AA001
AA001.aa Q3 2022 aa
AA Q4 2022 aa
BB Q4 2022 bb
Doing
df1 = df.replace(to_replace = "AA101” , value = “AA001”)
I keep getting syntax error, I am still researching. Any suggestion is appreciated.
CodePudding user response:
I think you want str.replace
.
You can either apply it for each column:
df = df.apply(lambda col: col.str.replace('AA101', 'AA001'))
or stack
the columns, use it; then unstack
it:
df = df.stack().str.replace('AA101', 'AA001').unstack()
Output:
ID Date Stat
0 AA001.aa Q3 2022 AA001
1 AA001.aa Q3 2022 aa
2 AA Q4 2022 aa
3 BB Q4 2022 bb
CodePudding user response:
Lets try this
df.replace(regex=r'AA101', value='AA001')
ID Date Stat
0 AA001.aa Q3 2022 AA001
1 AA001.aa Q3 2022 aa
2 AA Q4 2022 aa
3 BB Q4 2022 bb