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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
import streamlit as st
|
| 152 |
import pandas as pd
|
| 153 |
import io
|
|
@@ -225,8 +424,10 @@ def main():
|
|
| 225 |
writer = pd.ExcelWriter(excel_buffer, engine='xlsxwriter')
|
| 226 |
workbook = writer.book
|
| 227 |
|
|
|
|
|
|
|
|
|
|
| 228 |
# Loop through each triplet of columns, process each as a separate DataFrame, and save to an Excel sheet
|
| 229 |
-
# Loop through selected columns in sets of three (Question, Response, Frequency)
|
| 230 |
for i in range(0, len(df_selected.columns), 3):
|
| 231 |
# Ensure we have complete triplets of columns (question, response, frequency)
|
| 232 |
if i + 2 < len(df_selected.columns):
|
|
@@ -243,7 +444,14 @@ def main():
|
|
| 243 |
|
| 244 |
# Ensure a valid sheet name
|
| 245 |
valid_sheet_name = "".join([c for c in question_text if c.isalnum() or c in [' ', '_']]).replace(" ", "_")
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
# Write the DataFrame to an Excel sheet
|
| 248 |
df_triplet.to_excel(writer, sheet_name=valid_sheet_name[:31], index=False)
|
| 249 |
|
|
@@ -295,7 +503,6 @@ def main():
|
|
| 295 |
'categories': categories_range,
|
| 296 |
'values': values_range,
|
| 297 |
'data_labels': {
|
| 298 |
-
|
| 299 |
'value': True, # Show value on the bars
|
| 300 |
'font': {'size': 14, 'bold': False, 'color': '#36516E'} # Set font size to 14 and color to #36516E
|
| 301 |
},
|
|
|
|
| 148 |
|
| 149 |
# if __name__ == "__main__":
|
| 150 |
# main()
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
# import streamlit as st
|
| 154 |
+
# import pandas as pd
|
| 155 |
+
# import io
|
| 156 |
+
# import xlsxwriter
|
| 157 |
+
|
| 158 |
+
# def main():
|
| 159 |
+
# st.title("My Response Refiner")
|
| 160 |
+
|
| 161 |
+
# # Example text
|
| 162 |
+
# st.write("Hello, this is an app specialized for sorting and visualizing the customers' responses!")
|
| 163 |
+
|
| 164 |
+
# # File upload section
|
| 165 |
+
# uploaded_file = st.file_uploader("Upload your data file", type=["csv", "xlsx"])
|
| 166 |
+
|
| 167 |
+
# if uploaded_file is not None:
|
| 168 |
+
# try:
|
| 169 |
+
# # Check file type and read the file accordingly
|
| 170 |
+
# if uploaded_file.name.endswith(".csv"):
|
| 171 |
+
# df = pd.read_csv(uploaded_file)
|
| 172 |
+
# elif uploaded_file.name.endswith(".xlsx"):
|
| 173 |
+
# df = pd.read_excel(uploaded_file)
|
| 174 |
+
|
| 175 |
+
# # Set Color Palette for Later Use
|
| 176 |
+
# colors = ['#36516E', '#D6DCE5', '#99CCE0', '#A5A5A5', '#74A9E8', '#636363'] # Adjust as needed for more data points
|
| 177 |
+
|
| 178 |
+
# # Get all columns that start with "Question"
|
| 179 |
+
# question_columns = [col for col in df.columns if col.startswith("Question")]
|
| 180 |
+
|
| 181 |
+
# # Create a dictionary to store the new column names
|
| 182 |
+
# new_column_names = {}
|
| 183 |
+
|
| 184 |
+
# # Iterate through the question columns
|
| 185 |
+
# for col in question_columns:
|
| 186 |
+
# # Get the first non-null value in the column
|
| 187 |
+
# first_value = df[col].dropna().iloc[0] if not df[col].dropna().empty else col
|
| 188 |
+
|
| 189 |
+
# # Create the new column name for the Question column
|
| 190 |
+
# new_column_names[col] = f"Question: {first_value}"
|
| 191 |
+
|
| 192 |
+
# # Find the index of the current column
|
| 193 |
+
# col_index = df.columns.get_loc(col)
|
| 194 |
+
|
| 195 |
+
# # Check if there's a next column and rename it
|
| 196 |
+
# if col_index + 1 < len(df.columns):
|
| 197 |
+
# next_col = df.columns[col_index + 1]
|
| 198 |
+
# new_column_names[next_col] = f"Response: {first_value}"
|
| 199 |
+
|
| 200 |
+
# # Rename the columns
|
| 201 |
+
# df.rename(columns=new_column_names, inplace=True)
|
| 202 |
+
|
| 203 |
+
# list_of_columns = list(df.columns)
|
| 204 |
+
# # Multiselect dropdown to allow user to choose the columns to keep
|
| 205 |
+
# columns_to_keep = st.multiselect("Select the columns you want to keep:", list_of_columns)
|
| 206 |
+
|
| 207 |
+
# if columns_to_keep:
|
| 208 |
+
# # Create a new DataFrame with only the selected columns
|
| 209 |
+
# df_selected = df[columns_to_keep]
|
| 210 |
+
# # Get all columns that start with "Response: "
|
| 211 |
+
# response_columns = [col for col in df_selected.columns if col.startswith("Response:")]
|
| 212 |
+
|
| 213 |
+
# # Iterate through the response columns
|
| 214 |
+
# for i, col in enumerate(response_columns):
|
| 215 |
+
# # Count the frequency of each response in this specific column
|
| 216 |
+
# frequency = df_selected[col].value_counts()
|
| 217 |
+
# col2 = col.replace("Response: ", "")
|
| 218 |
+
# # Create a new column name for the frequency
|
| 219 |
+
# freq_col_name = f"Frequency: {col2}"
|
| 220 |
+
|
| 221 |
+
# # Add the frequency column right next to the response column
|
| 222 |
+
# insert_position = df_selected.columns.get_loc(col) + 1
|
| 223 |
+
# df_selected.insert(insert_position, freq_col_name, df_selected[col].map(frequency))
|
| 224 |
+
|
| 225 |
+
# # Create a BytesIO object to save the Excel file in memory
|
| 226 |
+
# excel_buffer = io.BytesIO()
|
| 227 |
+
# writer = pd.ExcelWriter(excel_buffer, engine='xlsxwriter')
|
| 228 |
+
# workbook = writer.book
|
| 229 |
+
|
| 230 |
+
# # Loop through each triplet of columns, process each as a separate DataFrame, and save to an Excel sheet
|
| 231 |
+
# # Loop through selected columns in sets of three (Question, Response, Frequency)
|
| 232 |
+
# for i in range(0, len(df_selected.columns), 3):
|
| 233 |
+
# # Ensure we have complete triplets of columns (question, response, frequency)
|
| 234 |
+
# if i + 2 < len(df_selected.columns):
|
| 235 |
+
# question_col = df_selected.columns[i]
|
| 236 |
+
# response_col = df_selected.columns[i + 1]
|
| 237 |
+
# frequency_col = df_selected.columns[i + 2]
|
| 238 |
+
|
| 239 |
+
# # Get the question text (after "Question: " prefix)
|
| 240 |
+
# question_text = question_col.replace("Question: ", "").strip()
|
| 241 |
+
|
| 242 |
+
# # Prepare the DataFrame for this triplet
|
| 243 |
+
# df_triplet = df_selected[[question_col, response_col, frequency_col]].drop_duplicates(subset=[response_col])
|
| 244 |
+
# df_triplet = df_triplet.sort_values(by=frequency_col, ascending=False)
|
| 245 |
+
|
| 246 |
+
# # Ensure a valid sheet name
|
| 247 |
+
# valid_sheet_name = "".join([c for c in question_text if c.isalnum() or c in [' ', '_']]).replace(" ", "_")
|
| 248 |
+
|
| 249 |
+
# # Write the DataFrame to an Excel sheet
|
| 250 |
+
# df_triplet.to_excel(writer, sheet_name=valid_sheet_name[:31], index=False)
|
| 251 |
+
|
| 252 |
+
# # Create a worksheet for this triplet
|
| 253 |
+
# worksheet = writer.sheets[valid_sheet_name[:31]]
|
| 254 |
+
|
| 255 |
+
# # Add chart selection option
|
| 256 |
+
# chart_type = st.selectbox("Select chart type for analysis:", ["Pie Chart", "Bar Chart"], key=f"chart_type_{i//3}")
|
| 257 |
+
|
| 258 |
+
# # Create the appropriate chart based on the user's selection
|
| 259 |
+
# if chart_type == "Pie Chart":
|
| 260 |
+
# # Create a pie chart object
|
| 261 |
+
# chart = workbook.add_chart({'type': 'pie'})
|
| 262 |
+
|
| 263 |
+
# # Define the data for the chart
|
| 264 |
+
# categories_range = f"='{valid_sheet_name[:31]}'!B2:B{len(df_triplet) + 1}" # Response column (Category names)
|
| 265 |
+
# values_range = f"='{valid_sheet_name[:31]}'!C2:C{len(df_triplet) + 1}" # Frequency column
|
| 266 |
+
|
| 267 |
+
# # Add series to the chart with Category Names (responses), values, and percentages
|
| 268 |
+
# chart.add_series({
|
| 269 |
+
# 'categories': categories_range,
|
| 270 |
+
# 'values': values_range,
|
| 271 |
+
# 'data_labels': {
|
| 272 |
+
# 'category': True, # Show Category Names (Response)
|
| 273 |
+
# 'percentage': True, # Show percentage
|
| 274 |
+
# 'value': False, # Show value
|
| 275 |
+
# 'position': 'outside_end', # Position labels outside the pie chart
|
| 276 |
+
# 'font': {'size': 14, 'bold': False, 'color': '#36516E'} # Set font size to 14 and color to #36516E
|
| 277 |
+
# },
|
| 278 |
+
# 'points': [{'fill': {'color': color}} for color in colors[:len(df_triplet)]], # Apply the colors
|
| 279 |
+
# })
|
| 280 |
+
|
| 281 |
+
# # Disable the chart legend
|
| 282 |
+
# chart.set_legend({'none': True})
|
| 283 |
+
|
| 284 |
+
# # Insert the chart into the worksheet
|
| 285 |
+
# worksheet.insert_chart('E5', chart)
|
| 286 |
+
|
| 287 |
+
# elif chart_type == "Bar Chart":
|
| 288 |
+
# # Create a bar chart object
|
| 289 |
+
# chart = workbook.add_chart({'type': 'column'})
|
| 290 |
+
|
| 291 |
+
# # Define the data for the chart
|
| 292 |
+
# categories_range = f"='{valid_sheet_name[:31]}'!B2:B{len(df_triplet) + 1}" # Response column (Category names)
|
| 293 |
+
# values_range = f"='{valid_sheet_name[:31]}'!C2:C{len(df_triplet) + 1}" # Frequency column
|
| 294 |
+
|
| 295 |
+
# # Add series to the chart with Category Names (responses) and values
|
| 296 |
+
# chart.add_series({
|
| 297 |
+
# 'categories': categories_range,
|
| 298 |
+
# 'values': values_range,
|
| 299 |
+
# 'data_labels': {
|
| 300 |
+
|
| 301 |
+
# 'value': True, # Show value on the bars
|
| 302 |
+
# 'font': {'size': 14, 'bold': False, 'color': '#36516E'} # Set font size to 14 and color to #36516E
|
| 303 |
+
# },
|
| 304 |
+
# 'legend_key': False # Don't show the legend key
|
| 305 |
+
# })
|
| 306 |
+
|
| 307 |
+
# # Set the chart title
|
| 308 |
+
# # chart.set_title({'name': f"Frequency of Responses for {question_text}"})
|
| 309 |
+
|
| 310 |
+
# # Set x-axis title
|
| 311 |
+
# chart.set_x_axis({
|
| 312 |
+
# 'visible': True, # Hide x-axis titles
|
| 313 |
+
# 'major_gridlines': {'visible': False}, # Remove horizontal gridlines
|
| 314 |
+
# 'num_font': {'size': 14}
|
| 315 |
+
# })
|
| 316 |
+
|
| 317 |
+
# # Set y-axis title
|
| 318 |
+
# chart.set_y_axis({
|
| 319 |
+
# 'visible': False,
|
| 320 |
+
# 'num_font': {'size': 14},
|
| 321 |
+
# 'major_gridlines': {'visible': False},
|
| 322 |
+
# })
|
| 323 |
+
|
| 324 |
+
# # Disable the chart legend
|
| 325 |
+
# chart.set_legend({'none': True})
|
| 326 |
+
|
| 327 |
+
# # Insert the chart into the worksheet
|
| 328 |
+
# worksheet.insert_chart('E5', chart)
|
| 329 |
+
|
| 330 |
+
# # Save the Excel file
|
| 331 |
+
# writer.close()
|
| 332 |
+
# excel_buffer.seek(0)
|
| 333 |
+
|
| 334 |
+
# # Create a download button
|
| 335 |
+
# st.download_button(
|
| 336 |
+
# label="Download Excel file",
|
| 337 |
+
# data=excel_buffer,
|
| 338 |
+
# file_name="converted_document.xlsx",
|
| 339 |
+
# mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 340 |
+
# )
|
| 341 |
+
|
| 342 |
+
# st.success("File uploaded and processed successfully!")
|
| 343 |
+
|
| 344 |
+
# except Exception as e:
|
| 345 |
+
# st.error(f"Error: {e}")
|
| 346 |
+
|
| 347 |
+
# if __name__ == "__main__":
|
| 348 |
+
# main()
|
| 349 |
+
|
| 350 |
import streamlit as st
|
| 351 |
import pandas as pd
|
| 352 |
import io
|
|
|
|
| 424 |
writer = pd.ExcelWriter(excel_buffer, engine='xlsxwriter')
|
| 425 |
workbook = writer.book
|
| 426 |
|
| 427 |
+
# Keep track of used sheet names
|
| 428 |
+
used_sheet_names = {}
|
| 429 |
+
|
| 430 |
# Loop through each triplet of columns, process each as a separate DataFrame, and save to an Excel sheet
|
|
|
|
| 431 |
for i in range(0, len(df_selected.columns), 3):
|
| 432 |
# Ensure we have complete triplets of columns (question, response, frequency)
|
| 433 |
if i + 2 < len(df_selected.columns):
|
|
|
|
| 444 |
|
| 445 |
# Ensure a valid sheet name
|
| 446 |
valid_sheet_name = "".join([c for c in question_text if c.isalnum() or c in [' ', '_']]).replace(" ", "_")
|
| 447 |
+
|
| 448 |
+
# If sheet name is already used, append a unique number to avoid duplication
|
| 449 |
+
if valid_sheet_name in used_sheet_names:
|
| 450 |
+
used_sheet_names[valid_sheet_name] += 1
|
| 451 |
+
valid_sheet_name = f"{valid_sheet_name}_{used_sheet_names[valid_sheet_name]}"
|
| 452 |
+
else:
|
| 453 |
+
used_sheet_names[valid_sheet_name] = 1
|
| 454 |
+
|
| 455 |
# Write the DataFrame to an Excel sheet
|
| 456 |
df_triplet.to_excel(writer, sheet_name=valid_sheet_name[:31], index=False)
|
| 457 |
|
|
|
|
| 503 |
'categories': categories_range,
|
| 504 |
'values': values_range,
|
| 505 |
'data_labels': {
|
|
|
|
| 506 |
'value': True, # Show value on the bars
|
| 507 |
'font': {'size': 14, 'bold': False, 'color': '#36516E'} # Set font size to 14 and color to #36516E
|
| 508 |
},
|