我想根据列的绝对值以升序或降序对极坐标数据框进行排序。Pandas在Python中或者使用sortedPython中的函数很容易做到。假设我想根据val下面的数据框中的列进行排序。
import numpy as np\nnp.random.seed(42)\nimport polars as pl\n\ndf = pl.DataFrame({\n "name": ["one", "one", "one", "two", "two", "two"],\n "id": ["C", "A", "B", "B", "C", "C"],\n "val": np.random.randint(-10, 10, 6)\n })\nRun Code Online (Sandbox Code Playgroud)\n返回:
\n\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xac\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n\xe2\x94\x82 name \xe2\x94\x86 id \xe2\x94\x86 val \xe2\x94\x82\n\xe2\x94\x82 --- \xe2\x94\x86 --- \xe2\x94\x86 --- \xe2\x94\x82\n\xe2\x94\x82 str \xe2\x94\x86 str \xe2\x94\x86 i32 \xe2\x94\x82\n\xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xaa\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1\n\xe2\x94\x82 one \xe2\x94\x86 C \xe2\x94\x86 -4 \xe2\x94\x82\n\xe2\x94\x82 one \xe2\x94\x86 A \xe2\x94\x86 9 \xe2\x94\x82\n\xe2\x94\x82 one \xe2\x94\x86 B \xe2\x94\x86 4 \xe2\x94\x82\n\xe2\x94\x82 two \xe2\x94\x86 B \xe2\x94\x86 …Run Code Online (Sandbox Code Playgroud) 我正在尝试回调另一个类中的类方法。如果我在创建对象时不定义变量 x,y,z (请参阅注释部分),它就可以正常工作。但是,如果我显式定义变量名称,则它不起作用。想知道是什么导致了这种情况发生?
class ClassA():
def __init__(self, a, b):
self.a = a
self.b = b
def method_a(self):
return f'A, method_a, {self.a}, {self.b}'
def callback_method(self, *args):
obj = ClassB(*args)
return obj.method_b()
class ClassB():
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def method_b(self):
return f'B, method_b, {self.x}, {self.y}, {self.z}'
A = ClassA(a=1, b=2)
print(A.method_a())
# A, method_a, 1, 2
B = ClassB(x=3, y=4, z=5)
print(B.method_b())
# B, method_b, 3, 4, 5
print(A.callback_method(10, 11, 12))
# …Run Code Online (Sandbox Code Playgroud)