1

I need to replace the specific values in every row of pandas df with another value. My data looks like this:

time        log
1         whats the weather look like today
2         what is the weather look like today
3         whats for lunch
4         what’s for lunch

I need to replace whats to be what is and what’s to be what is also. The desired output:

time        log
1         what is the weather look like today
2         what is the weather look like today
3         what is for lunch
4         what is for lunch

I have tried:

new_df = df.log.str.replace("^whats", "what is").str.replace("^what’s", "what is")

This took care of whats but not the other case and the outcome is not a pandas df and I need it to be pandas df.

12
  • 3
    df['log'] = df.log.str.replace("^whats|what’s", "what is")? Commented Feb 9, 2021 at 19:14
  • it does return the dataframe back but it doesn't take care of the replacement. Commented Feb 9, 2021 at 19:22
  • 2
    I have a feeling that there might be some problem with what exactly the characters are. Because as I have just updated, it works for me. Can you find the give rows if you search for the string? Commented Feb 9, 2021 at 19:35
  • 1
    Copied the text in your example and the replacement method - it works for me. Commented Feb 9, 2021 at 19:54
  • 1
    Are you sure you only have string there and no byte code? Or can you post the original data, the ones which make the trouble? What I mean is that what you see as ’s might be totally different thing which is just shown like this, hence if you copy it out as it is here, all works, but in your dataset the actual character is not ’s. Your finding suggest that, for me it find the row with ’s. Commented Feb 9, 2021 at 20:35

1 Answer 1

2

What you are getting is a Pandas Series, if you want to get a DataFrame, just use

new_df = pd.DataFrame(df.log.str.replace("^whats", "what is").str.replace("^what’s", "what is"))

And as was pointed out by @Quang Hoang, you can search using the pandas OR and search either for whats or what’s:

new_df = pd.DataFrame(df.log.str.replace("^whats|what’s", "what is"))

Full code:

import pandas as pd

df = pd.DataFrame({"time": [1,2,3,4],
    "log": ['whats the weather look like today', 'what is the weather look like today', 'whats for lunch', 'what’s for lunch']
             })
new_df = pd.DataFrame(df.log.str.replace("^whats", "what is").str.replace("^what’s", "what is"))

and the results are:

print(df)
   time                                  log
0     1    whats the weather look like today
1     2  what is the weather look like today
2     3                      whats for lunch
3     4                   what’s for lunch

print(new_df)
                                   log
0  what is the weather look like today
1  what is the weather look like today
2                    what is for lunch
3                    what is for lunch

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response, this does return the dataframe, however, it does not replace what’s
It does for me, I will update the answer with the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.