<jupyter_start><jupyter_text>Handling missing Data  1. Fill na Values<jupyter_code>import pandas as pd
df = pd.read_csv("weather_data.csv",parse_dates=['day'])
print(type(df.day[0])) #parse-date actually convert day column to date types
df
new_df = df.fillna(0)                      #Fill na values
new_df
new_df = df.fillna({                  #Fill na values using dictionary
        'temperature': 0,
        'windspeed': 0,
        'event': 'No Event'
    })
new_df
new_df = df.fillna(method="ffill")         #fillna using method='ffill' (forward fill)
new_df
new_df = df.fillna(method="bfill", axis="columns") # axis is either "index" or "columns"
new_df                                             #this time fillna using backward fill using row
new_df = df.fillna(method="ffill",limit=1) #using limit in fillna function
new_df<jupyter_output><empty_output><jupyter_text>2. Interpolate<jupyter_code>new_df = df.interpolate()         #using interpolate 
new_df                            #by default use linear interpolation
df.set_index('day',inplace=True)
new_df = df.interpolate(method="time")  #interpolate using time method
new_df                                  #time method making better guess #considering dates for temperatures 32>>29>>28<jupyter_output><empty_output><jupyter_text>3. Drop na Values<jupyter_code>new_df = df.dropna()
new_df                           #Droping every column which have at least one na value
new_df = df.dropna(how='all')     #Droping every column which row have na values for all
new_df                            # Using "how" parameters in dropna
new_df = df.dropna(thresh=2)     #using thresh parameter in dropna
new_df                           #Droping every column which have at least two na values
dt = pd.date_range("01-01-2017","01-11-2017")                 # If We want to use every missing dates
idx = pd.DatetimeIndex(dt)      #Using pandas DatetimeIndex
df.reindex(idx)                 #reindexing<jupyter_output><empty_output><jupyter_text>4. Data Replace<jupyter_code>import numpy as np
df = pd.read_csv("weather_data2.csv")
df
new_df = df.replace(-99999, value=np.NaN)  #replacing -99999 with NaN values
new_df                                     #np.NaN
new_df = df.replace(to_replace=[-99999,-88888], value=0) #using to_replace passing list for replacing
new_df
new_df = df.replace({                      #passing dictionary  # replacing specific column values
        'temperature': -99999,
        'windspeed': -99999,
        'event': '0'
    }, np.nan)    

new_df
new_df = df.replace({                     # Replacing by mapping
        -99999: np.nan,                   # change to specific values for specific values
        'no event': 'Sunny',
    })
new_df
# when windspeed is 6 mph, 7 mph etc. & temperature is 32 F, 28 F etc.
new_df = df.replace({'temperature': '[A-Za-z]', 'windspeed': '[a-z]'},'', regex=True) # Using regular expression 
new_df                                  # Removing a,b,c,d from numbers
df = pd.DataFrame({
    'score': ['exceptional','average', 'good', 'poor', 'average', 'exceptional'],
    'student': ['rob', 'maya', 'parthiv', 'tom', 'julian', 'erica']
})
df
df.replace(['poor', 'average', 'good', 'exceptional'], [1,2,3,4]) # Replacing with another list<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1177007299270073, 'fraction_numerical': 0.024330900243309004, 'mean_word_length': 2.7375, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '243944', 'license': 'MIT', 'n_tokens_mistral': 1091, 'n_tokens_neox': 1018, 'n_words': 289, 'path': 'Pandas/4. Handling missing Data.ipynb', 'repo_name': 'Shaon2221/Learning-and-Experimenting_Data-Science', 'size': '3288'}