我定义了一个类,它的__ge__方法返回自身的一个实例,并且__bool__不允许调用其方法(类似于 Pandas Series)。
为什么X.__bool__在 during 中被调用np.int8(0) <= x,而不是在其他任何示例中被调用?谁在调用它?我已经阅读了数据模型文档,但我还没有在那里找到答案。
import numpy as np
import pandas as pd
class X:
def __bool__(self):
print(f"{self}.__bool__")
assert False
def __ge__(self, other):
print(f"{self}.__ge__")
return X()
x = X()
np.int8(0) <= x
# Console output:
# <__main__.X object at 0x000001BAC70D5C70>.__ge__
# <__main__.X object at 0x000001BAC70D5D90>.__bool__
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "<stdin>", line 4, in __bool__ …Run Code Online (Sandbox Code Playgroud) 试图建立关分拣Python字典的建议在这里,我将如何去打印基于该值的绝对值排序顺序Python字典?
我试过了:
sorted(mydict, key=abs(mydict.get))
Run Code Online (Sandbox Code Playgroud)
但这会引发错误bad operand type for abs(): 'builtin_function_or_method'abs()期望一个数字,而不是一个函数.另外,abs()是函数abs的返回值,而key只是期望一个函数.
我有一个像这样的数据帧:
Subject Verb Object Date
---------------------------------
Bill Ate Food 7/11/2015
Steve Painted House 8/12/2011
Bill Ate Food 7/13/2015
Steve Painted House 8/25/2011
Run Code Online (Sandbox Code Playgroud)
我想删除所有重复项,其中重复项被定义为具有相同的主语、动词、宾语,并且在 X 天范围内(在我的示例中:5 天)。
Subject Verb Object Date
---------------------------------
Bill Ate Food 7/11/2015
Steve Painted House 8/12/2011
Steve Painted House 8/25/2011
Run Code Online (Sandbox Code Playgroud)
“Steve - Painted - House”的两个实例都没有被移除,因为它们在 5 天的窗口之外。
我知道我可以使用一些数据结构和 DataFrame 的 iterrows 方法来做到这一点,但是有没有办法使用 Pandas drop_duplicates 做到这一点?
python ×3
pandas ×2
dataframe ×1
dictionary ×1
duplicates ×1
numpy ×1
python-3.x ×1
sorting ×1