I am trying to remove extra char from panda dataframe and I have created a method to do that. But while calling from another method it does not work

Question:

I have two data frames and I am trying to remove all the columns to lower case and remove any - if there are any. When trying to call the remove_extrachar from compare it is not removing any - or making it lower-case. However, when printing in remove_extrachar, I can see it is working.

def remove_extrachar(df1, df2):

    df1 = df1.apply(lambda x: x.astype(str).str.lower().str.replace('-', ''))
    df2 = df2.apply(lambda x: x.astype(str).str.lower().str.replace('-', ''))

def compare(df1, df2):

    remove_extrachar(df1, df2)
    compare =pd.merge(df1, df2, on='name', how='outer')
Asked By: Developer

||

Answers:

It might not be working because you’re not returning anything. can you try this:

def remove_extrachar(df1, df2):

    df1 = df1.apply(lambda x: x.astype(str).str.lower().str.replace('-', ''))
    df2 = df2.apply(lambda x: x.astype(str).str.lower().str.replace('-', ''))
    return (df1,df2)

def compare(df1, df2):

    df1,df2 = remove_extrachar(df1, df2)
    compare = pd.merge(df1, df2, on='name', how='outer')
    return compare
    
final=compare(df1, df2)
Answered By: Clegane
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.