-1

Here is my requirement

I have an existing data frame df.A[a,b,c] and would like create a new df.B[X,Y] from the df.A by doing some arithmetic operation on the columns in df.A

it will be like

df.A= a b c
      0 1 2
      0 2 0
      1 3 2

My df.B will be derived as

df.B['X','Y']=df.A[(sum[A.a]+[A.b]),sum[A.c]]

The output should look like

df.B= X Y
      7 4

let me know if you need any further details to achieve this case.

manda1238
  • 73
  • 1
  • 2
  • 9

1 Answers1

0

Is your only goal to get the sum of (A+B) and the sum of (C)?

If that's the case, just do something like this:

import pandas as pd
import numpy as np

df1 = pd.DataFrame({'A':np.random.randint(0,10,3),
                    'B':np.random.randint(0,10,3),
                    'C':np.random.randint(0,10,3)})
df1.groupby(df1.index.isin(['A','B'])).sum().rename({False: 'Y', True: 'X'})

I'm having a hard time seeing the purpose of this, so if you add more context, it'd be helpful for creating a more elegant solution.

I think it'd be easier to just get outputs:

A_plus_B = df3[['A','B']].to_numpy().sum()
C = df3['C'].sum()
Jadon
  • 124
  • 9
  • I have to do some arithmetic operation on the data which is in my df.A , and assign the result to the new columns which will in df.B I have one other field called d in my df.A which i will use a group function , if i have to write a sql query , select (sum(a)+sum(b)) as x,sum(c) as y from A group by d . – manda1238 Aug 03 '20 at 18:02
  • I'm having a really difficult time understanding that last bit. As long as you update the data ahead of time before you generate df.A, performing the operation I wrote out should incorporate it. It'd be easiest to explain the last thing you want it to do by showing an example input dataframe and what you want the output to be. – Jadon Aug 03 '20 at 18:53