Search code examples
pythonpandassplitdata-analysisattributeerror

Cleaning data frames with rogue elements using split()


Given the following data in an excel sheet (taken in as a dataframe) :

    Name  Number       Date
    AA    '9988779911' '01-JAN-18'
    'BB'  '8779912044' '01-FEB-18'

I have used the following code to clean the dataframe and remove the unnecessary apostrophes;

for name in list(df):
    df[name] = df[name].str.split("'").str[1]

And I want the following output :

    Name  Number       Date
    AA    9988779911   01-JAN-18
    BB    8779912044   01-FEB-18

I am getting the following error : AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

Thanks in advance for your help.:):)


Solution

  • Strip function is probably the shortest way here. The other answers are elegant too.

        str.strip("'")
    

    Moshevi has said the same in one of the comments.