File size: 586 Bytes
e93c178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import pandas as pd

def load_data(path):
    try:
        # read csv file
        df = pd.read_csv(path)

        # fill missing numeric values with mean
        for col in df.select_dtypes(include=['int64', 'float64']).columns:
            df[col].fillna(df[col].mean(), inplace=True)

        # fill missing categorical values with most frequent value
        for col in df.select_dtypes(include=['object']).columns:
            df[col].fillna(df[col].mode()[0], inplace=True)

        return df
    except:
        # return None if file not found or error occurs
        return None