Update pandas DataFrame with .str.replace() vs .replace()

前端 未结 2 876
醉话见心
醉话见心 2021-01-21 16:35

I have a column in my pandas Dataframe df that contains a string with some trailing hex-encoded NULLs (\\x00). At least I think that it\'s that. When I tried to replace them wit

2条回答
  •  暖寄归人
    2021-01-21 16:54

    You did not specify a regex or require an exact match, hence str.replace worked

    str.replace(old, new[, count])
    

    Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

    DataFrame.replace(to_replace=None, value=None, inplace=False, limit=None, regex=False, method='pad', axis=None)
    
    parameter: to_replace : str, regex, list, dict, Series, numeric, or None
    

    str or regex: str: string exactly matching to_replace will be replaced with value regex: regexs matching to_replace will be replaced with value

    They're not actually in the string: you have unescaped control characters, which Python displays using the hexadecimal notation:

    remove all non-word characters in the following way:

    re.sub(r'[^\w]', '', '\x00\x00\x00\x08\x01\x008\xe6\x7f')
    

提交回复
热议问题