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')
CodePudding user response:
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)