Informatics Practices
Give two identical DataFrames Sales16 and Sales17. But Sales17 has some values missing. Write code so that Sales17 fills its missing values from the corresponding entries of Sales16.
Python Pandas
1 Like
Answer
import pandas as pd
data_sales16 = {
'Product': ['A', 'B', 'C', 'D'],
'Sales': [100, 150, 120, 180]
}
Sales16 = pd.DataFrame(data_sales16)
data_sales17 = {
'Product': ['A', 'B', 'C', 'D'],
'Sales': [100, None, 120, None]
}
Sales17 = pd.DataFrame(data_sales17)
Sales17 = Sales16.fillna({'B': 150, 'D': 180})
print("Sales16:")
print(Sales16)
print("\nSales17 (after filling missing values):")
print(Sales17)Output
Sales16:
Product Sales
0 A 100
1 B 150
2 C 120
3 D 180
Sales17 (after filling missing values):
Product Sales
0 A 100
1 B 150
2 C 120
3 D 180
Answered By
3 Likes
Related Questions
Write a program that performs count, sum, max, and min functions :
On rows
On columns
Take a DataFrame of your choice. Write a program to calculate count of values only in a selective column.
Write code that just produces single True/False as a result for the presence of missing values in whole DataFrame namely df.
Four Series objects Temp1, Temp2, Temp3 and Temp4 store the temperatures of week1, week2, week3 and week4 respectively. Create a DataFrame from these four series objects where the indexes should be 'Sunday', 'Monday', … , 'Saturday', and columns should be 'Week1', 'Week2', 'Week3' and 'week4'.