简体   繁体   中英

How to Modify the existing dataframe passed to a function

I m trying to modify the same dataframe that I pass to a function but the changes are not visible

when I apply changes to the dataframe outside the function the expected results is achieved

test_df = test_df.apply(lambda x: x.str.strip("\t") if x.dtype == "object" else x)

printing out unique values -

print ("{0}-->{1}".format(val,pd.unique(test_df[val])))

O/P-

htn-->['yes' 'no']   
dm-->['yes' 'no' '  yes']  
cad-->['no' 'yes']
appet-->['good' 'poor']  
pe-->['no' 'yes']  
ane-->['no' 'yes']  
classification-->['ckd' 'notckd']

However if I pass the Dataframe to a function and apply the above same functions the changes are not observed

def FillMissing(dataFrame):
    dataFrame = dataFrame.apply(lambda x: x.str.strip("\t") if x.dtype == "object" else x)


FillMissing(test_df)

O/P-

htn-->['yes' 'no']  
dm-->['yes' 'no' ' yes' '\tno' '\tyes']  
cad-->['no' 'yes' '\tno']  
appet-->['good' 'poor']  
pe-->['no' 'yes']  
ane-->['no' 'yes']  
classification-->['ckd' 'ckd\t' 'notckd']

How can I modify the same existing dataframe without declaring it a global variable.

Also I have tried inplace flag with the lambda function , it does not work

What you are facing is a typical dilemma of beginner pythonistas. Look at this example

a = 10
b = a
b = 11

When b is assigned the value of a , b and a are in fact pointing to an object created to store the value 10 ( Everything is an object in Python ). But when b is assigned a new value 11 , a is not assigned the value since it is still pointing to the old value 10 . Only the pointer b has changed

So when you do

def FillMissing(dataFrame):
dataFrame = dataFrame.apply(lambda x: x.str.strip("\t") if x.dtype == "object" else x)


FillMissing(test_df)

Thought test_df and dataFrame initially have the same value, dataFrame is later reassigned the result of the dataFrame.apply() operation but test_df returns the same. A solution to this is to return the new dataFrame from the method, either singly or as a tuple if required.

A nice diagrammatic reference

http://foobarnbaz.com/2012/07/08/understanding-python-variables/

...But in Python variables work more like tags unlike the boxes you have seen before. When you do an assignment in Python, it tags the value with the variable name....Other languages have 'variables'. Python has 'names'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM