Informatics Practices
Create a dataframe of [23, 25], [34], [43, 44, 45, 46] and do the following: :
(a) Display the dataframe. Notice that the missing value is represented by NaN.
(b) Replace the missing value with 0.
(c) Replace the missing value with -1, -2, -3, -4 for columns 0, 1, 2, 3.
(d) Replace the missing value by copying the value from the above cell.
Python Data Handling
1 Like
Answer
(a)
import pandas as pd
data = [[23, 25],
[34],
[43, 44, 45, 46]]
df = pd.DataFrame(data)
print(df)
Output
0 1 2 3
0 23 25.0 NaN NaN
1 34 NaN NaN NaN
2 43 44.0 45.0 46.0
(b)
df = df.fillna(0)
Output
0 1 2 3
0 23 25.0 0.0 0.0
1 34 0.0 0.0 0.0
2 43 44.0 45.0 46.0
(c)
df = df.fillna({0:-1, 1:-2, 2:-3, 3:-4})
Output
0 1 2 3
0 23 25.0 -3.0 -4.0
1 34 -2.0 -3.0 -4.0
2 43 44.0 45.0 46.0
(d)
df= df.fillna(method='ffill')
Output
0 1 2 3
0 23 25.0 NaN NaN
1 34 25.0 NaN NaN
2 43 44.0 45.0 46.0
Answered By
2 Likes
Related Questions
Create two dataframes of salary of five employees and do the following:
(a) Display both the dataframes.
(b) Add 5000 as bonus in both dataframes and display them.
Create a dataframe using list [10, 11, 12, 13, 14] [23, 34, 45, 32, 65] [55, 60, 65, 70, 75] and do the following:
(a) Display the dataframe.
(b) Add the list [1, 2, 3, 4, 5] to dataframe and display it.
Create a dataframe of D1 and D2;
D1 = {‘Rollno’ : [1001, 1004, 1005, 1008, 1009], ‘Name’: [‘Sarika’, ‘Abhay’, ‘Mohit’, ‘Ruby’, ‘Govind’ ]}
D2 = {‘Rollno’ : [1002, 1003, 1004, 1005, 1006], ‘Name’:[‘Seema’,‘Jia’,‘Shweta’, ‘Sonia’, ‘Nishant’]}(a) Concatenate row-wise.
(b) Concatenate column-wise.
Create a dataframe of {‘A’ : [ ]} and display whether it is empty or not.