Divide multiple columns by another column in pandas

PythonPandas

Python Problem Overview


I need to divide all but the first columns in a DataFrame by the first column.

Here's what I'm doing, but I wonder if this isn't the "right" pandas way:

df = pd.DataFrame(np.random.rand(10,3), columns=list('ABC'))

df[['B', 'C']] = (df.T.iloc[1:] / df.T.iloc[0]).T

Is there a way to do something like df[['B','C']] / df['A']? (That just gives a 10x12 dataframe of nan.)

Also, after reading some similar questions on SO, I tried df['A'].div(df[['B', 'C']]) but that gives a broadcast error.

Python Solutions


Solution 1 - Python

I believe df[['B','C']].div(df.A, axis=0) and df.iloc[:,1:].div(df.A, axis=0) work.

Solution 2 - Python

do: df.iloc[:,1:] = df.iloc[:,1:].div(df.A, axis=0)

This will divide all columns other than the 1st column with the 'A' column used as divisor.

Results are 1st column + all columns after / 'divisor column'.

Solution 3 - Python

You are actually doing a matrix multiplication (Apparently numpy understands that "/" operator multiplies by the inverse), so you need the shapes to match (see here).

e.g.

df['A'].shape --> (10,)
df[['B','C']].shape --> (10,2)

You should make them match as (2,10)(10,):
df[['B','C']].T.shape, df['A'].shape -->((2, 10), (10,))

But then your resulting matrix is: ( df[['B','C']].T / df['A'] ).shape --> (2,10)

Therefore:

( df[['B','C']].T / df['A'] ).T

Shape is (10,2). It gives you the results that you wanted!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionitzyView Question on Stackoverflow
Solution 1 - PythonDthalView Answer on Stackoverflow
Solution 2 - Pythonuser17300673View Answer on Stackoverflow
Solution 3 - PythonaerijmanView Answer on Stackoverflow