简体   繁体   中英

Pandas sort_values() inside a function. How to allow user to choose a column to sort by? Or perhaps leave blank

I want to create a function by default sorts_by nothing. But the user has the option to specify a column to sort on.

Sample df:

df= pd.DataFrame({'col1':['mary','john','patrick','michael'],
                 'col2':[1,2,3,4]})
print(df)

      col1  col2
0     mary     1
1     john     2
2  patrick     3
3  michael     4

Sample function, multiplies a column by two.

Has a sort_by argument that takes the column that will be sorted, but I would like no sorting by default.

My None is causing an error.

def multiply(df,sortby=None):

    # multiply column by 2
    df.col2 = df.col2*2 

    # sort by user choice of column, default no sort
    df.sort_values(by=sortby,inplace=True) 

    print(df)

Now running the function:

multiply(df)

KeyError: None

What can I put into this line: df.sort_values(by=sortby,inplace=True) that just defaults to no sorting? Is it possible to leave it 'blank' somehow? I tried sorting by df.index by default but that's also not possible.

I know I can do it by maybe adding a Boolean argument that the user can specify eg if sort==True: sort_values(by=column) else: no sorting

But I would just like to know specifically if it's possible to set a default by= value for sort_values(by=) that would allow it to remain unsorted, or perhaps sorted by index, or with the original sorting it entered the function.

I hope I'm making sense. This was a bit hard to explain.

You need a simple if statement:

def multiply(df,sortby=None):

    # multiply column by 2
    df.col2 = df.col2*2 

    # sort by user choice of column, default no sort
    if sortby is not None:
        df.sort_values(by=sortby,inplace=True) 

    print(df)

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