1.

Write the code in pandas to create the following dataframes : df1   df2mark1 mark2mark1 mark20 10 150 30 201 40 45 1 20 252 15 302 20 303 40 703 50 30Write the commands to do the following operations on the dataframes given above :(i) To add dataframes df1 and df2.(ii) To subtract df2 from df1 (iii) To rename column mark1 as marks1in both the dataframes df1 and df2.(iv) To change index label of df1 from 0 to zero and from 1 to one.

Answer»

import numpy as np

import pandas as pd

df1 = pd.DataFrame({'mark1':[30,40,15,40],

'mark2':[20,45,30,70]});

df2 = pd.DataFrame({'mark1':[10,20,20,50],

'mark2':[15,25,30,30]});

print(df1)

print(df2)

(i)  print(df1.add(df2))

(ii)  print(df1.subtract(df2))

(iii)  df1.rename(columns={'mark1':'marks1'}, inplace=True) print(df1)

(iv)  df1.rename(index = {0: "zero", 1:"one"}, inplace = True) print(df1) 



Discussion

No Comment Found

Related InterviewSolutions