Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,132 +1,164 @@
|
|
| 1 |
-
import os
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
import
|
| 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 |
if __name__ == "__main__":
|
| 132 |
-
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import os
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import seaborn as sns
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
import tabula
|
| 10 |
+
from tabula import read_pdf
|
| 11 |
+
except:
|
| 12 |
+
read_pdf = None
|
| 13 |
+
|
| 14 |
+
# ----------- File Upload Handler ----------- #
|
| 15 |
+
def file_upload(file):
|
| 16 |
+
file_ext = os.path.splitext(file.name)[1].lower()
|
| 17 |
+
try:
|
| 18 |
+
if file_ext == '.csv':
|
| 19 |
+
df = pd.read_csv(file)
|
| 20 |
+
elif file_ext in ['.xls', '.xlsx']:
|
| 21 |
+
df = pd.read_excel(file)
|
| 22 |
+
elif file_ext == '.json':
|
| 23 |
+
df = pd.read_json(file)
|
| 24 |
+
elif file_ext == '.pdf' and read_pdf:
|
| 25 |
+
df = read_pdf(file, pages='all', multiple_tables=False)[0]
|
| 26 |
+
else:
|
| 27 |
+
st.error("β Unsupported file type or missing dependencies for PDF.")
|
| 28 |
+
return None
|
| 29 |
+
return df
|
| 30 |
+
except Exception as e:
|
| 31 |
+
st.error(f"β οΈ Error loading file: {e}")
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
# ----------- Cleaning Functions ----------- #
|
| 35 |
+
def remove_empty_rows(df):
|
| 36 |
+
st.info("π Null values before cleaning:")
|
| 37 |
+
st.write(df.isnull().sum())
|
| 38 |
+
df_cleaned = df.dropna()
|
| 39 |
+
st.success("β
Null values removed.")
|
| 40 |
+
return df_cleaned
|
| 41 |
+
|
| 42 |
+
def replace_nulls(df, value):
|
| 43 |
+
st.info("π Null values before replacement:")
|
| 44 |
+
st.write(df.isnull().sum())
|
| 45 |
+
df_filled = df.fillna(value)
|
| 46 |
+
st.success("β
Null values replaced.")
|
| 47 |
+
return df_filled
|
| 48 |
+
|
| 49 |
+
def remove_noise(df):
|
| 50 |
+
noise_words = {'the', 'is', 'an', 'a', 'in', 'of', 'to'}
|
| 51 |
+
def clean_text(val):
|
| 52 |
+
if isinstance(val, str):
|
| 53 |
+
return ' '.join(word for word in val.split() if word.lower() not in noise_words)
|
| 54 |
+
return val
|
| 55 |
+
df_cleaned = df.applymap(clean_text)
|
| 56 |
+
st.success("β
Noise words removed.")
|
| 57 |
+
return df_cleaned
|
| 58 |
+
|
| 59 |
+
def remove_duplicates(df):
|
| 60 |
+
df_deduped = df.drop_duplicates()
|
| 61 |
+
st.success("β
Duplicate rows removed.")
|
| 62 |
+
return df_deduped
|
| 63 |
+
|
| 64 |
+
def convert_column_dtype(df, column, dtype):
|
| 65 |
+
try:
|
| 66 |
+
df[column] = df[column].astype(dtype)
|
| 67 |
+
st.success(f"β
Converted '{column}' to {dtype}")
|
| 68 |
+
except Exception as e:
|
| 69 |
+
st.error(f"β οΈ Conversion error: {e}")
|
| 70 |
+
return df
|
| 71 |
+
|
| 72 |
+
def detect_outliers(df, column):
|
| 73 |
+
if column in df.select_dtypes(include=['float', 'int']).columns:
|
| 74 |
+
Q1 = df[column].quantile(0.25)
|
| 75 |
+
Q3 = df[column].quantile(0.75)
|
| 76 |
+
IQR = Q3 - Q1
|
| 77 |
+
lower = Q1 - 1.5 * IQR
|
| 78 |
+
upper = Q3 + 1.5 * IQR
|
| 79 |
+
outliers = df[(df[column] < lower) | (df[column] > upper)]
|
| 80 |
+
st.write(f"π Found {len(outliers)} outliers in column '{column}'")
|
| 81 |
+
return outliers
|
| 82 |
+
else:
|
| 83 |
+
st.warning("β οΈ Column must be numeric to detect outliers.")
|
| 84 |
+
return pd.DataFrame()
|
| 85 |
+
|
| 86 |
+
def plot_distributions(df):
|
| 87 |
+
st.subheader("π Data Distributions")
|
| 88 |
+
numeric_cols = df.select_dtypes(include=['float', 'int']).columns
|
| 89 |
+
for col in numeric_cols:
|
| 90 |
+
fig, ax = plt.subplots()
|
| 91 |
+
sns.histplot(df[col].dropna(), kde=True, ax=ax)
|
| 92 |
+
ax.set_title(f"Distribution of {col}")
|
| 93 |
+
st.pyplot(fig)
|
| 94 |
+
|
| 95 |
+
def plot_missing_data(df):
|
| 96 |
+
st.subheader("π Missing Data Heatmap")
|
| 97 |
+
fig, ax = plt.subplots()
|
| 98 |
+
sns.heatmap(df.isnull(), cbar=False, cmap='viridis')
|
| 99 |
+
st.pyplot(fig)
|
| 100 |
+
|
| 101 |
+
def main():
|
| 102 |
+
st.set_page_config(page_title="π§Ή Smart Dataset Cleaner", layout="wide")
|
| 103 |
+
st.title("π§Ή Smart Dataset Cleaner")
|
| 104 |
+
st.caption("β¨ Clean, analyze, and preprocess your dataset with ease")
|
| 105 |
+
|
| 106 |
+
uploaded_file = st.file_uploader("π Upload your dataset", type=["csv", "xlsx", "xls", "json", "pdf"])
|
| 107 |
+
if uploaded_file:
|
| 108 |
+
df = file_upload(uploaded_file)
|
| 109 |
+
if df is not None:
|
| 110 |
+
st.subheader("π Original Dataset Preview")
|
| 111 |
+
st.dataframe(df.head())
|
| 112 |
+
|
| 113 |
+
st.markdown("## π§° Data Cleaning Tools")
|
| 114 |
+
with st.expander("β Replace Null Values"):
|
| 115 |
+
fill_value = st.text_input("Enter value to replace nulls with:")
|
| 116 |
+
if st.button("Replace Nulls"):
|
| 117 |
+
df = replace_nulls(df, fill_value)
|
| 118 |
+
st.dataframe(df)
|
| 119 |
+
|
| 120 |
+
if st.button("π§Ό Remove Empty Rows"):
|
| 121 |
+
df = remove_empty_rows(df)
|
| 122 |
+
st.dataframe(df)
|
| 123 |
+
|
| 124 |
+
if st.button("π§Ή Remove Duplicate Rows"):
|
| 125 |
+
df = remove_duplicates(df)
|
| 126 |
+
st.dataframe(df)
|
| 127 |
+
|
| 128 |
+
if st.button("π Remove Noise Words from Text"):
|
| 129 |
+
df = remove_noise(df)
|
| 130 |
+
st.dataframe(df)
|
| 131 |
+
|
| 132 |
+
with st.expander("π Convert Column DataType"):
|
| 133 |
+
selected_col = st.selectbox("Select column", df.columns)
|
| 134 |
+
dtype = st.selectbox("Select target type", ["int", "float", "str", "bool"])
|
| 135 |
+
if st.button("Convert"):
|
| 136 |
+
df = convert_column_dtype(df, selected_col, dtype)
|
| 137 |
+
st.dataframe(df)
|
| 138 |
+
|
| 139 |
+
st.markdown("## π Data Visualizations")
|
| 140 |
+
if st.checkbox("π Show Summary Stats"):
|
| 141 |
+
st.write(df.describe(include='all'))
|
| 142 |
+
|
| 143 |
+
if st.checkbox("π Plot Column Distributions"):
|
| 144 |
+
plot_distributions(df)
|
| 145 |
+
|
| 146 |
+
if st.checkbox("π Show Missing Data Heatmap"):
|
| 147 |
+
plot_missing_data(df)
|
| 148 |
+
|
| 149 |
+
st.markdown("## π¨ Outlier Detection")
|
| 150 |
+
outlier_col = st.selectbox("Select numeric column", df.select_dtypes(include=['float', 'int']).columns)
|
| 151 |
+
if st.button("Detect Outliers"):
|
| 152 |
+
outliers = detect_outliers(df, outlier_col)
|
| 153 |
+
if not outliers.empty:
|
| 154 |
+
st.write(outliers)
|
| 155 |
+
|
| 156 |
+
st.markdown("## πΎ Download Cleaned Dataset")
|
| 157 |
+
file_name = st.text_input("Filename:", "cleaned_dataset.csv")
|
| 158 |
+
if st.button("Download CSV"):
|
| 159 |
+
st.download_button("π Download", df.to_csv(index=False), file_name, mime="text/csv")
|
| 160 |
+
else:
|
| 161 |
+
st.warning("β οΈ Please upload a supported file to begin.")
|
| 162 |
+
|
| 163 |
if __name__ == "__main__":
|
| 164 |
+
main()
|