Spaces:
Sleeping
Sleeping
File size: 7,817 Bytes
5c802bc |
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
import streamlit as st
import pandas as pd
import numpy as np
import io
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
import seaborn as sns
import base64
def handle_categorical_values():
if "data" in st.session_state:
data = st.session_state["data"]
st.subheader("Handle Categorical Values")
categorical_cols_features = list(
data.select_dtypes(include="object").columns)
# One-Hot Encoding for nominal categorical features
one_hot_enc = st.multiselect(
"Select nominal categorical columns", categorical_cols_features)
# Apply one-hot encoding to selected columns
if one_hot_enc:
for column in one_hot_enc:
if data[column].dtype == 'object': # Only apply to categorical/string columns
data = pd.get_dummies(data, columns=[column])
st.write("### Data after One-Hot Encoding:")
st.write(data.head())
# Label Encoding for ordinal categorical features
label_encoder = LabelEncoder()
label_enc = st.multiselect(
"Select ordinal categorical columns", categorical_cols_features)
# Apply label encoding to selected columns
if label_enc:
for column in label_enc:
if data[column].dtype == 'object': # Only apply to categorical/string columns
data[column] = label_encoder.fit_transform(data[column])
st.write("### Data after Label Encoding:")
st.write(data.head())
else:
st.warning("Please upload a dataset to handle categorical values.")
def missing_values():
st.title("Handle Missing Values")
if "data" in st.session_state:
data = st.session_state["data"].copy()
action = st.selectbox(
"Select Action", ["Drop", "Dropna", "Fill missing val"])
column = st.selectbox("Select Column", data.columns)
# Before Visualization
st.write("### Before:")
st.dataframe(data)
# Placeholder for After Visualization
after_placeholder = st.empty()
if st.button("OK"):
modified_data = data.copy()
if action == "Drop":
modified_data.drop(columns=[column], inplace=True)
elif action == "Dropna":
modified_data.dropna(subset=[column], inplace=True)
elif action == "Fill missing val":
fill_method = st.selectbox(
"Select fill method", ["Mean", "Mode", "Median"])
if fill_method == "Mean":
fill_value = data[column].mean()
elif fill_method == "Mode":
fill_value = data[column].mode()[0]
elif fill_method == "Median":
fill_value = data[column].median()
modified_data[column].fillna(fill_value, inplace=True)
# After Visualization
after_placeholder.write("### After:")
after_placeholder.dataframe(modified_data)
st.session_state["data"] = modified_data
else:
st.warning("Please upload a dataset first.")
def handle_duplicates():
st.title("Handle Duplicates")
if "data" in st.session_state:
data = st.session_state["data"].copy()
action = st.selectbox(
"Select Action", ["Drop Duplicates", "Drop Duplicates in Column", "Keep First", "Keep Last"])
if action in ["Drop Duplicates in Column", "Keep First", "Keep Last"]:
column = st.selectbox("Select Column", data.columns)
else:
column = None
# Before Visualization
st.write("### Before:")
st.dataframe(data)
# Placeholder for After Visualization
after_placeholder = st.empty()
if st.button("OK"):
modified_data = data.copy()
if action == "Drop Duplicates":
modified_data.drop_duplicates(inplace=True)
elif action == "Drop Duplicates in Column":
modified_data.drop_duplicates(subset=[column], inplace=True)
elif action == "Keep First":
# Keep the first occurrence of duplicates and drop others
modified_data.drop_duplicates(
subset=[column], keep="first", inplace=True)
elif action == "Keep Last":
# Keep the last occurrence of duplicates and drop others
modified_data.drop_duplicates(
subset=[column], keep="last", inplace=True)
# After Visualization
after_placeholder.write("### After:")
after_placeholder.dataframe(modified_data)
st.session_state["data"] = modified_data
else:
st.warning("Please upload a dataset first.")
def handle_outliers():
st.title("Handle Outliers")
if "data" in st.session_state:
data = st.session_state["data"].copy()
column = st.selectbox("Select Column", data.select_dtypes(
include=[np.number]).columns)
action = st.selectbox(
"Select Action",
["Remove Outliers (IQR)", "Set Bounds Manually",
"Replace Outliers"]
)
st.write("### Before:")
st.dataframe(data)
after_placeholder = st.empty()
if st.button("OK"):
modified_data = data.copy()
if action == "Remove Outliers (IQR)":
Q1 = data[column].quantile(0.25)
Q3 = data[column].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
# Remove outliers
modified_data = modified_data[
(modified_data[column] >= lower_bound) & (
modified_data[column] <= upper_bound)
]
elif action == "Set Bounds Manually":
# User inputs for bounds
lower_bound = st.number_input(
f"Set lower bound for {column}", value=float(data[column].min()))
upper_bound = st.number_input(
f"Set upper bound for {column}", value=float(data[column].max()))
# Remove rows outside the bounds
modified_data = modified_data[
(modified_data[column] >= lower_bound) & (
modified_data[column] <= upper_bound)
]
elif action == "Replace Outliers":
Q1 = data[column].quantile(0.25)
Q3 = data[column].quantile(0.75)
IQR = Q3 - Q1
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
replace_method = st.radio(
"Select Replacement Method",
["Mean", "Median"]
)
if replace_method == "Mean":
replacement_value = data[column].mean()
else:
replacement_value = data[column].median()
# Replace outliers
modified_data[column] = modified_data[column].apply(
lambda x: replacement_value if x < lower_bound or x > upper_bound else x
)
after_placeholder.write("### After:")
after_placeholder.dataframe(modified_data)
st.session_state["data"] = modified_data
else:
st.warning("Please upload a dataset first.")
|