File size: 2,943 Bytes
3c540f6
 
 
 
 
b12c1f6
 
 
 
 
 
 
 
 
3c540f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0fd0ed
3c540f6
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import io
import base64

st.set_page_config(page_title="Data Cleaning Tool")

hide_streamlit_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            </style>
            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)

st.title("CSV Data Cleaning Tool")

st.markdown("Upload one or multiple CSV files to preprocess and clean your files quickly and stress free.")

uploaded_files = st.file_uploader("Choose CSV files", type="csv", accept_multiple_files=True)

dataframes = []

if uploaded_files:
    for file in uploaded_files:
        file.seek(0)
        df = pd.read_csv(file)
        dataframes.append(df)

    if len(dataframes) > 1:
        merge = st.checkbox("Merge uploaded CSV files")

        if merge:
            # Merge options
            keep_first_header_only = st.selectbox("Keep only the header (first row) of the first file", ["Yes", "No"])
            remove_duplicate_rows = st.selectbox("Remove duplicate rows", ["No", "Yes"])
            remove_empty_rows = st.selectbox("Remove empty rows", ["Yes", "No"])
            end_line = st.selectbox("End line", ["\\n", "\\r\\n"])

            try:
                if keep_first_header_only == "Yes":
                    for i, df in enumerate(dataframes[1:]):
                        df.columns = dataframes[0].columns.intersection(df.columns)
                        dataframes[i+1] = df

                merged_df = pd.concat(dataframes, ignore_index=True, join='outer')

                if remove_duplicate_rows == "Yes":
                    merged_df.drop_duplicates(inplace=True)

                if remove_empty_rows == "Yes":
                    merged_df.dropna(how="all", inplace=True)

                dataframes = [merged_df]

            except ValueError as e:
                st.error("Please make sure columns match in all files. If you don't want them to match, select 'No' in the first option.")
                st.stop()

    # Show or hide DataFrames
    show_dataframes = st.checkbox("Show DataFrames", value=True)

    if show_dataframes:
        for i, df in enumerate(dataframes):
            st.write(f"DataFrame {i + 1}")
            st.dataframe(df)

    if st.button("Download cleaned data"):
        for i, df in enumerate(dataframes):
            csv = df.to_csv(index=False)
            b64 = base64.b64encode(csv.encode()).decode()
            href = f'<a href="data:file/csv;base64,{b64}" download="cleaned_data_{i + 1}.csv">Download cleaned_data_{i + 1}.csv</a>'
            st.markdown(href, unsafe_allow_html=True)
else:
    st.warning("Please upload CSV file(s).")
    st.stop()

st.markdown("")
st.markdown("---")
st.markdown("")
st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)