Answer to Question #241039 in Python for saksham

Question #241039
How to create a new column in pandas by using values from other columns?
We can perform column based mathematical operations on a pandas dataframe. Pandas columns containing numeric values can be operated upon by operators.

Code

import pandas as pd
a=[1,2,3]
b=[2,3,5]
d={"col1":a,"col2":b}
df=pd.DataFrame(d)
df["Sum"]=df["col1"]+df["col2"]
df["Difference"]=df["col1"]-df["col2"]
How to create a new column in pandas by using values from other columns?
We can perform column based mathematical operations on a pandas dataframe. Pandas columns containing numeric values can be operated upon by operators.
1
Expert's answer
2021-09-23T17:47:00-0400
import pandas as pd

a=[1,2,3]

b=[2,3,5]

d={"col1":a,"col2":b}

df=pd.DataFrame(d)

print(df)

df["Sum"]=df["col1"]+df["col2"]

df["Difference"]=df["col1"]-df["col2"]

df["new_column"]=df["Sum"]*df["Difference"]

print(df)

Output:

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS