Spaces:
Sleeping
Sleeping
File size: 2,693 Bytes
b523dd4 |
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 |
import pandas as pd
import streamlit as st
def transform_data(df):
# Transform 'respond' variable
respond_mapping = {
"Parent": 1, "Teacher": 2, "Self": 3, "Other": 4,
"Significant other": 5, "Parent 1": 6, "Parent 2": 7,
"Not available": 999
}
if 'respond' in df:
df['respond'] = df['respond'].map(respond_mapping)
# Transform 'sri_ts' and 'sld_ts' variables
sri_sld_values = {
8: 36.6, 9: 42.1, 10: 44.8, 11: 46.8, 12: 48.5, 13: 50.0, 14: 51.3,
15: 52.5, 16: 53.7, 17: 54.9, 18: 56.0, 19: 57.1, 20: 58.2, 21: 59.3,
22: 60.3, 23: 61.4, 24: 62.4, 25: 63.5, 26: 64.5, 27: 65.6, 28: 66.6,
29: 67.6, 30: 68.7, 31: 69.7, 32: 70.7, 33: 71.8, 34: 72.9, 35: 74.1,
36: 75.4, 37: 76.8, 38: 78.5, 39: 80.3, 40: 82.7
}
if 'sri_rs' in df.columns:
df['sri_ts'] = df['sri_rs'].map(sri_sld_values).fillna("NA")
if 'sld_rs' in df.columns:
df['sld_ts'] = df['sld_rs'].map(sri_sld_values).fillna("NA")
# Transform 'dsm_cross_ch' variables
dsm_cross_ch_cols = [f'dsm_cross_ch{num}' for num in range(20, 26)]
for col in dsm_cross_ch_cols:
if col in df:
df[col] = df[col].map({0: 2, 1: 1})
# Transform 'dsm_cross_pg' variables
dsm_cross_pg_cols = [f'dsm_cross_pg{num}' for num in range(20, 26)]
for col in dsm_cross_pg_cols:
if col in df:
df[col] = df[col].map({0: 1, 1: 2, 2: -9})
# Transform 'rcads_y' variables
rcads_y_cols = [f'rcads_y{num}' for num in range(14, 27)]
for col in rcads_y_cols:
if col in df:
df[col] = df[col] + 1
# Ensure rcads_y26 exists and set default values
if 'rcads_y26' not in df:
df['rcads_y26'] = 1
# Return transformed dataframe
return df
def main():
st.title("Data Transformation App")
uploaded_file = st.file_uploader("Upload CSV or Excel file", type=['csv', 'xlsx'])
if uploaded_file:
# Determine the file type and read data accordingly
if uploaded_file.name.endswith('.csv'):
df = pd.read_csv(uploaded_file)
elif uploaded_file.name.endswith('.xlsx'):
df = pd.read_excel(uploaded_file)
# Transform the data
transformed_df = transform_data(df)
# Display transformed data
st.write("Transformed Data:")
st.dataframe(transformed_df)
# Download link for transformed data
st.download_button(
label="Download Transformed Data",
data=transformed_df.to_csv(index=False).encode('utf-8'),
file_name='transformed_data.csv',
mime='text/csv',
)
if __name__ == '__main__':
main() |