问题
In attempt to answer another question I've been playing around with column-wise multiplication operations in pandas.
A = pd.DataFrame({'Col1' : [1, 2, 3], 'Col2' : [2, 3, 4]})
B = pd.DataFrame({'Col1' : [10, 20, 30]})
print(A)
Col1 Col2
0 1 2
1 2 3
2 3 4
print(B)
Col1
0 10
1 20
2 30
I tried to use df.apply
in an attempt to multiply Col1
of B
with each column of A. So my desired output is:
Col1 Col2
0 10 20
1 40 60
2 90 120
My first attempt was to use a lambda
and it worked fine.
df_new = A.apply(lambda x: B.Col1.values * x, 0)
print(df_new)
Col1 Col2
0 10 20
1 40 60
2 90 120
But lambdas are always slow, so I thought I could speed this up with passing B.col1.values.__mul__
instead, but this is what it gave:
print(A.apply(B.Col1.values.__mul__, 0))
Col1 NotImplemented
Col2 NotImplemented
dtype: object
I printed out __mul__
, all it is is a magic method for multiplication in numpy arrays:
print(B.Col1.values.__mul__)
<method-wrapper '__mul__' of numpy.ndarray object at 0x1154d9620>
Why do I get this error?
回答1:
You can just do:
A.apply(B.Col1.__mul__,0)
Which returns what you're after.
The difference is that B.Col1.values.__mul__
is calling the numpy slot function, but B.Col1.__mul__
is calling a pandas method.
Likely the pandas method was written to avoid some low level headache from numpy:
>>>print(inspect.getsource(pd.Series.__mul__))
def wrapper(left, right, name=name, na_op=na_op):
if isinstance(right, pd.DataFrame):
return NotImplemented
left, right = _align_method_SERIES(left, right)
converted = _Op.get_op(left, right, name, na_op)
left, right = converted.left, converted.right
lvalues, rvalues = converted.lvalues, converted.rvalues
dtype = converted.dtype
wrap_results = converted.wrap_results
na_op = converted.na_op
if isinstance(rvalues, ABCSeries):
name = _maybe_match_name(left, rvalues)
lvalues = getattr(lvalues, 'values', lvalues)
rvalues = getattr(rvalues, 'values', rvalues)
# _Op aligns left and right
else:
name = left.name
if (hasattr(lvalues, 'values') and
not isinstance(lvalues, pd.DatetimeIndex)):
lvalues = lvalues.values
result = wrap_results(safe_na_op(lvalues, rvalues))
return construct_result(
left,
result,
index=left.index,
name=name,
dtype=dtype,
)
Can't find source on the np slot function, but it's likely something similar to this
来源:https://stackoverflow.com/questions/45814262/pandas-v0-20-returns-notimplemented-when-multiplying-dataframe-columns