Professional Documents
Culture Documents
Missingvaluetreatment-Ex 2 Code
Missingvaluetreatment-Ex 2 Code
Vishwesh Singbal
# Reading a CSV file named "weather_data.csv" and parsing the 'day' column as datetime
>>df = pd.read_csv("weather_data.csv", parse_dates=['day'])
# Checking the data type of the first element in the 'day' column
>>type(df.day[0])
# Creating a new DataFrame 'new_df' by filling missing values with specified values
>>new_df = df.fillna({
'temperature': 0,
'windspeed': 0,
'event': 'No Event'
})
>>new_df
# Creating a new DataFrame 'new_df' by dropping rows with any missing values
>>new_df = df.dropna()
>>new_df
# Creating a new DataFrame 'new_df' by dropping rows where all values are missing
>>new_df = df.dropna(how='all')
>>new_df
# Creating a new DataFrame 'new_df' by dropping rows with less than 1 non-null value
>>new_df = df.dropna(thresh=1)
>>new_df
# Creating a DatetimeIndex 'idx' for the date range from "01-01-2017" to "01-11-2017"
>>dt = pd.date_range("01-01-2017", "01-11-2017")
>>idx = pd.DatetimeIndex(dt)