How to perform element wise operation on two sets of columns in pandas

1545 views python
0

I have the dataframe:

 c1 | c2 | c3 | c4
 5  |  4 | 9  | 3

How could I perform element wise division (or some other operation) between c1/c2 and c3/c4

So that the outcome is:

.5555 | 1.33333

I've tried:

df[['c1', 'c2']].div(df[['c3', 'c4']], axis='index'))

But that just resulted in NaNs.

answered question

What is that outcome supposed to be? A new dataframe? Without columns?

Yes, a dataframe with 2 columns, one for the outcome of c1/c3 and the other for the outcome of c2/c4. Sorry I wasn't clear.

1 Answer

13

I'm not positive I'm understanding your question correctly , but you can literally just divide the series.

df['c1/c2'] = df['c1'] / df['c2']

See this answer: How to divide two column in a dataframe

posted this

Have an answer?

JD

Please login first before posting an answer.