Assume for example that I am writing a function for the following code (in pandas module):
myData.sort_values(by=myVariable, ascending=False)
Now, I want to create a function whose variables are data, variable and ascending.
sort(data=mydata, variable=myvariable, ascending=False)
The below function gives an error, because whether ascending is true or false is not given in second line:
def sort(data,variable,ascending=False):
data.sort_values(by=variable, ascending)
Although it would work, I do not want to change variable name such as:
def sort(data,variable,asc=False):
data.sort_values(by=variable, ascending=asc)
One last approach would be to create a variable inside the function:
def sort(data,variable,ascending=False):
asc = ascending
data.sort_values(by=variable, ascending=asc)
but it looks somewhat confusing. Is there an alternative approach to use the same variable in this case?
Note: the question is not related with pandas module, it is just an example.
Edit: I have clearly stated my problem and showed what I have tried. I did not understand the reason for the downvote.