Q. Consider Data Frames pdf and pdf2 as shown below:
DataFrame pdf :-
Sean Shadab Sia Simran
L1 295.0 600.0 505 NaN
L2 397.0 NaN 600 397.0
L3 NaN 800.0 700 500.0
DataFrame pdf2 :-
Sean Shadab Sia Simran
L1 295.0 600.0 400 200.0
L2 397.0 200.0 600 397.0
L3 400.0 NaN 500 NaN
Write a program to fill the missing values in pdf with corresponding values.
Answer :-
import pandas as pd import numpy as np x = { 'Sean': {'L1': 295.0, 'L2' : 397.0, 'L3': np.NaN},\ 'Shadab' : {'L1': 600.0, 'L2':np.NaN, 'L3': 800.0},\ 'Sia': {'L1': 505, 'L2' : 600, 'L3' : 700},\ 'Simran': {'L1':np.NaN, 'L2': 397.0, 'L3': 500.0}} y = { 'Sean': {'L1': 295.0, 'L2' : 397.0, 'L3': 400},\ 'Shadab' : {'L1': 600.0, 'L2':200, 'L3': np.NaN},\ 'Sia': {'L1': 400, 'L2' : 600, 'L3' : 500},\ 'Simran': {'L1':200, 'L2': 397.0, 'L3': np.NaN}} pdf = pd.DataFrame (x) pdf2 = pd.DataFrame (y) print ("DataFrame pdf :-\n\n", pdf) print("\nDataframe pdf after filling values from datafrme pdf2\n") print (pdf.fillna(pdf2))
Output :-
DataFrame pdf :-
Sean Shadab Sia Simran
L1 295.0 600.0 505 NaN
L2 397.0 NaN 600 397.0
L3 NaN 800.0 700 500.0
Dataframe pdf after filling values from datafrme pdf2
Sean Shadab Sia Simran
L1 295.0 600.0 505 200.0
L2 397.0 200.0 600 397.0
L3 400.0 800.0 700 500.0
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )