Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading