-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed
Labels
Description
First of all, if I missed a point, please feel free to comment.
Using arithmetic operations on pd.DataFrames is sometimes a mouthful. Take the following example, where columns a
and b
should be multiplied by the column c
:
import numpy as np
import pandas as pd
np.random.seed(0)
df = pd.DataFrame(np.random.randn(3, 3), columns=list('abc'))
df[['a', 'b']] * df['c']
Apparently this doesn't work as expected. Instead one has to use either pd.Dataframe.mul()
, which brings up poor legibility, or pd.Dataframe.values
, which yields long lines and therefore also results in poor legibility:
# using pd.DataFrame.mul()
df[['a', 'b']].mul(df['c'], axis='index')
# This is quite short, but does not work...
df[['a', 'b']] * df[['c']].values
# .. you have to use numpy arrays instead
df[['a', 'b']].values * df[['c']].values
Surely, the last call in this example returns a numpy array, but in my case thats the only thing I'm interested in, since I'm rewrapping my data at a later stage.
I'm proposing a new short indexer for operating on values, sth like:
df.v[['a', 'b']] * df.v[['c']]
# which returns the same as
df[['a', 'b']].values * df[['c']].values
Or even more sophisticated:
df[['a', 'b']] * df.v[['c']]
# which returns the same as
df[['a', 'b']].mul(df['c'], axis='index')
Btw the same goes for all other arithmetic operators.