File size: 4,816 Bytes
a7bac1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""

Ashley Wu

Generates insights into the data given

"""
import pandas as pd
import gradio as gr
import utils

def calculate_stats(df):
    """

    Create a dataframe with each column's mean, median, max, min, quartiles for numeric columns and unique values, value counts, and mode for categorical columns as well as missing values 

    

    Args:

        dataframe: The pandas DataFrame to analyze

    

    Returns:

        A DataFrame with column statistics

    """
    df = utils.detect_categories(df)
    df = utils.detect_dates(df)
    
    if df is None or len(df) == 0:
        return None, None

    numeric_data = pd.DataFrame()
    categorical_data = pd.DataFrame() 

    for column in df.columns: 
        dtype = df[column].dtype
        if dtype.kind == 'i' or dtype.kind == 'f' or dtype.kind == 'u' or dtype.kind == 'c':
            numeric_data[column] = df[column]
        elif dtype.name == 'category':
            categorical_data[column] = df[column]
        elif dtype.kind == 'M' or dtype.kind == 'm':
            continue
        else: 
            gr.Warning(f"dType of {column} could not be recognised!", duration=5)
    
    numeric_data = calcualte_numeric_data(numeric_data)
    categorical_data = calculate_categorical_data(categorical_data)
    
    return numeric_data, categorical_data

def calculate_categorical_data(df): 
    """

    Calculates data for categorical data exploration

    

    Args: 

        df: dataframe

    Returns 

        table: dataframe

    """
    table = pd.DataFrame({
        'Column': pd.Series(dtype='string'),
        'Mode': pd.Series(dtype='string'),
        'Missing Values': pd.Series(dtype='int16'),
        'Value Counts': pd.Series(dtype='string'),
    })

    for column in df.columns: 
        mode = df[column].mode()[0]
        count = str(df[column].value_counts().to_dict())
        missing_values = df[column].isnull().sum()
        table.loc[len(table)] = [column, mode, missing_values, count]

    return table


def calcualte_numeric_data(df):
    """

    Calcualtes statistics for numerical data

    

    Args: 

        df: dataframe 

    Returns: 

        table: df with column statistics

    """
    table = pd.DataFrame({
        'Column': pd.Series(dtype='string'),
        'Mean':  pd.Series(dtype='float16'),
        'Median':  pd.Series(dtype='float16'),
        'Max':  pd.Series(dtype='float16'),
        'Min':  pd.Series(dtype='float16'),
        'Bottom Quartile':  pd.Series(dtype='float16'),
        'Top Quartile':  pd.Series(dtype='float16'),
        'Missing Values': pd.Series(dtype='int16'),
    })
    
    for column in df.columns:
        mean = df[column].mean()
        median = df[column].median()
        max_val = df[column].max()
        min_val = df[column].min()
        q25 = df[column].quantile(.25)
        q75 = df[column].quantile(.75)
        missing_values = df[column].isnull().sum()
        table.loc[len(table)] = [column, mean, median, max_val, min_val, q25, q75, missing_values]

    return table

def correlation_matrix(df): 
    """

    Create a correleation matrix for the given dataframe



    Args: 

        df: dataframe 

    Returns: 

        correlation_matrix: numpy array 

    """
    for col in df.columns:
        if df[col].dtype.name == 'object':
            df = df.drop(col, axis=1)
    return df.corr()


def max_min_df(df):
    """

    Find the max and min of a data frame

    

    Arg: 

        df: dataframe 

    Returns: 

        max_min_df: dataframe

    """
    for col in df.columns:
        if df[col].dtype.name == 'object':
            df = df.drop(col, axis=1)

    df_max = df.max()
    df_min = df.min()

    result = pd.DataFrame({
        'Column': df_max.index,
        'Max': df_max.values,
        'Min': df_min.values
    })
    return result

def std(df):
    """

    Finds the standard deviation of the dataframe 

    

    Args:

        df: dataframe 

    Returns: 

        std: dataframe 

    """
    for col in df.columns:
        if df[col].dtype.name == 'object':
            df = df.drop(col, axis=1)

    std_series = df.std()
    result = std_series.to_frame(name='Standard Deviation').reset_index()
    result.columns = ['Column', 'Standard Deviation']
    return result

def var(df):
    """

    Finds the variance of values in a dataframe 

    

    Args: 

        df: dataframe 

    Returns: 

        var: df

    """
    for col in df.columns:
        if df[col].dtype.name == 'object':
            df = df.drop(col, axis=1)
    var_series = df.var()
    result = var_series.to_frame(name='Variance').reset_index()
    result.columns = ['Column', 'Variance']
    return result