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
CodePudding user response:
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
CodePudding user response:
Another option along with the above answer, you can convert column 'C' to a numpy array by doing df[['C']].values
. Hence the new code would be:
df[['newA','newB']] = df[['A','B']]-df[['C']].values
CodePudding user response:
Try using Pandas .apply()
method. You can pass columns and apply a given function to them, in this case subtracting one of your existing columns. The below should work. Documentation here.
df[['newA','newB']] = df[['A','B']].apply(lambda x: x - df['C'])
CodePudding user response:
You can try to convert df['C'].values
to the same shape with df[['A','B']]
.
df[['newA','newB']] = df[['A','B']] - df['C'].values[:, None]
print(df)
A B C newA newB
0 10 100 15 -5 85
1 20 200 10 10 190
2 30 300 50 -20 250