[英]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.我想创建一个 function 默认
sorts_by
nothing。 But the user has the option to specify a column to sort on.但是用户可以选择指定要排序的列。
Sample df:样本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.示例 function,将一列乘以 2。
Has a sort_by
argument that takes the column that will be sorted, but I would like no sorting by default.有一个
sort_by
参数,它采用将被排序的列,但我不希望默认情况下进行排序。
My None
is causing an error.我的
None
导致错误。
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:现在运行 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?我可以在这一行中放入什么:
df.sort_values(by=sortby,inplace=True)
只是默认为不排序? Is it possible to leave it 'blank' somehow?是否有可能以某种方式将其“留空”? I tried sorting by
df.index
by default but that's also not possible.我尝试默认按
df.index
排序,但这也是不可能的。
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
我知道我可以通过添加用户可以指定的 Boolean 参数来做到这一点,例如
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.但我只想知道是否可以为
sort_values(by=)
设置默认by=
值,以使其保持未排序状态,或者按索引排序,或者使用原始排序进入 function。
I hope I'm making sense.我希望我说得通。 This was a bit hard to explain.
这有点难以解释。
You need a simple if
statement:你需要一个简单的
if
语句:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.