我正在尝试为具有 numpy 数组输入参数的函数制作一个缓存装饰器
from functools import lru_cache
import numpy as np
from time import sleep
a = np.array([1,2,3,4])
@lru_cache()
def square(array):
sleep(1)
return array * array
square(a)
Run Code Online (Sandbox Code Playgroud)
但是 numpy 数组不可散列,
TypeError Traceback (most recent call last)
<ipython-input-13-559f69d0dec3> in <module>()
----> 1 square(a)
TypeError: unhashable type: 'numpy.ndarray'
Run Code Online (Sandbox Code Playgroud)
所以它们需要转换为元组。我有这个工作和缓存正确:
@lru_cache()
def square(array_hashable):
sleep(1)
array = np.array(array_hashable)
return array * array
square(tuple(a))
Run Code Online (Sandbox Code Playgroud)
但我想把它全部包装在一个装饰器中,到目前为止我已经尝试过:
def np_cache(function):
def outter(array):
array_hashable = tuple(array)
@lru_cache()
def inner(array_hashable_inner):
array_inner = np.array(array_hashable_inner)
return function(array_inner)
return inner(array_hashable)
return outter
@np_cache …Run Code Online (Sandbox Code Playgroud) 我想pd.value_counts在MultiIndex中按级别对每列(据我估计)分组数据计数不同的值。multiindex使用groupby(level=参数来处理,但是apply会引发一个ValueError
原始数据框:
>>> df = pd.DataFrame(np.random.choice(list('ABC'), size=(10,5)),
columns=['c1','c2','c3','c4','c5'],
index=pd.MultiIndex.from_product([['foo', 'bar'],
['w','y','x','y','z']]))
c1 c2 c3 c4 c5
foo w C C B A A
y A A C B A
x A B C C C
y A B C C C
z A C B C B
bar w B C C A C
y A A C A A
x A B B B A
y A A C A B
z …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用过滤pandas数据框regular expressions。我想删除那些不包含任何字母的行。例如:
Col A.
50000
$927848
dog
cat 583
rabbit 444
Run Code Online (Sandbox Code Playgroud)
我想要的结果是:
Col A.
dog
cat 583
rabbit 444
Run Code Online (Sandbox Code Playgroud)
我一直在尝试使用regexand pandas过滤器选项解决不成功的问题。见打击。当我尝试合并过滤器的两个条件时,我特别遇到了问题。我该如何实现?
选项1:
df['Col A.'] = ~df['Col A.'].filter(regex='\d+')
Run Code Online (Sandbox Code Playgroud)
选项2
df['Col A.'] = df['Col A.'].filter(regex=\w+)
Run Code Online (Sandbox Code Playgroud)
选项3
from string import digits, letters
df['Col A.'] = (df['Col A.'].filter(regex='|'.join(letters)))
Run Code Online (Sandbox Code Playgroud)
要么
df['Col A.'] = ~(df['Col A.'].filter(regex='|'.join(digits)))
Run Code Online (Sandbox Code Playgroud)
要么
df['Col A.'] = df[~(df['Col A.'].filter(regex='|'.join(digits))) & (df['Col A.'].filter(regex='|'.join(letters)))]
Run Code Online (Sandbox Code Playgroud) python ×3
pandas ×2
caching ×1
dataframe ×1
decorator ×1
numpy ×1
python-2.7 ×1
python-3.x ×1
regex ×1