Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python dataframe substract a column from multiple columns

I have a dataframe and I want to substract a column from multiple columns
code:

df = pd.DataFrame('A':[10,20,30],'B':[100,200,300],'C':[15,10,50])

# Create a new A and B columns by sub-stracting C from A and B
df[['newA','newB']] = df[['A','B']]-df['C']

Present output:

    raise ValueError("cannot reindex from a duplicate axis")

ValueError: cannot reindex from a duplicate axis

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can check sub

df[['newA', 'newB']] = df[['A', 'B']].sub(df['C'],axis=0)
df
Out[114]: 
    A    B   C  newA  newB
0  10  100  15    -5    85
1  20  200  10    10   190
2  30  300  50   -20   250
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading