Search code examples
pythondataframereplacestr-replace

unbale to replace method in python


My problem is that I have to replcae the value in the remarked column and store it all in a new varble.

EXMP: - Wherever there is "True" in the Remark column, that "Data" gets updated. I have tried many times but I am not able to get many errors.

import pandas as pd

df1 = pd.DataFrame({'NOTES': ["PREPAID_HOME_SCREEN_MAMO","SCREEN_MAMO",
                              "> Unable to connect internet>4G Compatible>Set",
                              "No>Not Barred>Active>No>Available>Others>",
                              "Internet Not Working>>>Unable To Connect To"]})

df1['remark']= df1['NOTES'].str.contains('Internet')|df1['NOTES'].str.contains('MAMO')

df2 = df1['remark'].replace("True","Data")

my Try :- enter image description here


Solution

  • Currently you are trying to do a string replace on a boolean type.

    Try casting the column to a string first and then apply you replace method on that column:

    df1 = pd.DataFrame({'NOTES': ["PREPAID_HOME_SCREEN_MAMO", "SCREEN_MAMO",
                                  "> Unable to connect internet>4G Compatible>Set",
                                  "No>Not Barred>Active>No>Available>Others>",
                                  "Internet Not Working>>>Unable To Connect To"]})
    
    df1['remark'] = df1['NOTES'].str.contains('Internet') | df1['NOTES'].str.contains('MAMO')
    
    
    df2 = df1['remark'].astype(str).replace("True", "Data")