Spaces:
Sleeping
Sleeping
Rundstedtzz commited on
Commit ·
b523dd4
1
Parent(s): 699213b
upload app
Browse files- app.py +81 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
def transform_data(df):
|
| 5 |
+
# Transform 'respond' variable
|
| 6 |
+
respond_mapping = {
|
| 7 |
+
"Parent": 1, "Teacher": 2, "Self": 3, "Other": 4,
|
| 8 |
+
"Significant other": 5, "Parent 1": 6, "Parent 2": 7,
|
| 9 |
+
"Not available": 999
|
| 10 |
+
}
|
| 11 |
+
if 'respond' in df:
|
| 12 |
+
df['respond'] = df['respond'].map(respond_mapping)
|
| 13 |
+
|
| 14 |
+
# Transform 'sri_ts' and 'sld_ts' variables
|
| 15 |
+
sri_sld_values = {
|
| 16 |
+
8: 36.6, 9: 42.1, 10: 44.8, 11: 46.8, 12: 48.5, 13: 50.0, 14: 51.3,
|
| 17 |
+
15: 52.5, 16: 53.7, 17: 54.9, 18: 56.0, 19: 57.1, 20: 58.2, 21: 59.3,
|
| 18 |
+
22: 60.3, 23: 61.4, 24: 62.4, 25: 63.5, 26: 64.5, 27: 65.6, 28: 66.6,
|
| 19 |
+
29: 67.6, 30: 68.7, 31: 69.7, 32: 70.7, 33: 71.8, 34: 72.9, 35: 74.1,
|
| 20 |
+
36: 75.4, 37: 76.8, 38: 78.5, 39: 80.3, 40: 82.7
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
if 'sri_rs' in df.columns:
|
| 24 |
+
df['sri_ts'] = df['sri_rs'].map(sri_sld_values).fillna("NA")
|
| 25 |
+
|
| 26 |
+
if 'sld_rs' in df.columns:
|
| 27 |
+
df['sld_ts'] = df['sld_rs'].map(sri_sld_values).fillna("NA")
|
| 28 |
+
|
| 29 |
+
# Transform 'dsm_cross_ch' variables
|
| 30 |
+
dsm_cross_ch_cols = [f'dsm_cross_ch{num}' for num in range(20, 26)]
|
| 31 |
+
for col in dsm_cross_ch_cols:
|
| 32 |
+
if col in df:
|
| 33 |
+
df[col] = df[col].map({0: 2, 1: 1})
|
| 34 |
+
|
| 35 |
+
# Transform 'dsm_cross_pg' variables
|
| 36 |
+
dsm_cross_pg_cols = [f'dsm_cross_pg{num}' for num in range(20, 26)]
|
| 37 |
+
for col in dsm_cross_pg_cols:
|
| 38 |
+
if col in df:
|
| 39 |
+
df[col] = df[col].map({0: 1, 1: 2, 2: -9})
|
| 40 |
+
|
| 41 |
+
# Transform 'rcads_y' variables
|
| 42 |
+
rcads_y_cols = [f'rcads_y{num}' for num in range(14, 27)]
|
| 43 |
+
for col in rcads_y_cols:
|
| 44 |
+
if col in df:
|
| 45 |
+
df[col] = df[col] + 1
|
| 46 |
+
|
| 47 |
+
# Ensure rcads_y26 exists and set default values
|
| 48 |
+
if 'rcads_y26' not in df:
|
| 49 |
+
df['rcads_y26'] = 1
|
| 50 |
+
|
| 51 |
+
# Return transformed dataframe
|
| 52 |
+
return df
|
| 53 |
+
|
| 54 |
+
def main():
|
| 55 |
+
st.title("Data Transformation App")
|
| 56 |
+
|
| 57 |
+
uploaded_file = st.file_uploader("Upload CSV or Excel file", type=['csv', 'xlsx'])
|
| 58 |
+
if uploaded_file:
|
| 59 |
+
# Determine the file type and read data accordingly
|
| 60 |
+
if uploaded_file.name.endswith('.csv'):
|
| 61 |
+
df = pd.read_csv(uploaded_file)
|
| 62 |
+
elif uploaded_file.name.endswith('.xlsx'):
|
| 63 |
+
df = pd.read_excel(uploaded_file)
|
| 64 |
+
|
| 65 |
+
# Transform the data
|
| 66 |
+
transformed_df = transform_data(df)
|
| 67 |
+
|
| 68 |
+
# Display transformed data
|
| 69 |
+
st.write("Transformed Data:")
|
| 70 |
+
st.dataframe(transformed_df)
|
| 71 |
+
|
| 72 |
+
# Download link for transformed data
|
| 73 |
+
st.download_button(
|
| 74 |
+
label="Download Transformed Data",
|
| 75 |
+
data=transformed_df.to_csv(index=False).encode('utf-8'),
|
| 76 |
+
file_name='transformed_data.csv',
|
| 77 |
+
mime='text/csv',
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if __name__ == '__main__':
|
| 81 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|