Untitled

 avatar
unknown
python
17 days ago
2.9 kB
5
Indexable
# Calculate mean differences for key metrics
metrics = ['DASS_Depression', 'DASS_Angst', 'DASS_Stress', 'Schlaf Test', 
           'Schriftlicher Gedächtnistest1_numeric', 'Schriftlicher Gedächtnistest2_numeric']

mean_before = merged_data[[f"{metric}_before" for metric in metrics]].mean()
mean_after = merged_data[[f"{metric}_after" for metric in metrics]].mean()

# Combine mean values into a DataFrame for visualization
mean_comparison = pd.DataFrame({
    'Metric': metrics,
    'Mean_Before': mean_before.values,
    'Mean_After': mean_after.values
})

tools.display_dataframe_to_user(name="Mean Comparison Before and After Workshop", dataframe=mean_comparison)

# Visualize the mean comparison
import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))
for i, row in mean_comparison.iterrows():
    plt.bar(row['Metric'], row['Mean_Before'], label='Before', alpha=0.7, width=0.4, align='center')
    plt.bar(row['Metric'], row['Mean_After'], label='After', alpha=0.7, width=0.4, align='edge')

plt.xticks(rotation=45, ha='right')
plt.title('Mean Comparison Before and After Workshop')
plt.ylabel('Mean Score')
plt.xlabel('Metrics')
plt.legend(['Before', 'After'], loc='upper left')
plt.tight_layout()
plt.show()














# Fix the visualization to ensure consistent coloring for Before and After values
plt.figure(figsize=(10, 6))

# Define bar width
bar_width = 0.35

# Create x positions for the bars
x_positions = range(len(metrics))

# Plot bars with consistent coloring
plt.bar([x - bar_width / 2 for x in x_positions], mean_comparison['Mean_Before'], 
        width=bar_width, label='Before', color='blue', alpha=0.7)
plt.bar([x + bar_width / 2 for x in x_positions], mean_comparison['Mean_After'], 
        width=bar_width, label='After', color='orange', alpha=0.7)

# Add x-axis labels and title
plt.xticks(ticks=x_positions, labels=metrics, rotation=45, ha='right')
plt.title('Mean Comparison Before and After Workshop')
plt.ylabel('Mean Score')
plt.xlabel('Metrics')

# Add legend for clarity
plt.legend(loc='upper left')

# Improve layout and display
plt.tight_layout()
plt.show()







# Group data by gender and calculate mean differences for each gender
gender_grouped = merged_data.groupby('Geschlecht_before').mean()[[
    f"{metric}_before" for metric in metrics] + [f"{metric}_after" for metric in metrics]]

# Prepare the data for visualization
gender_grouped = gender_grouped.T
gender_grouped.columns = ['Male', 'Female']

# Plot mean differences by gender
gender_grouped.plot(kind='bar', figsize=(12, 6), color=['blue', 'orange'], alpha=0.7)

# Add labels and title
plt.title('Mean Comparison Before and After Workshop by Gender')
plt.ylabel('Mean Score')
plt.xlabel('Metrics')
plt.xticks(rotation=45, ha='right')
plt.legend(title='Gender', loc='upper left')

# Show the plot
plt.tight_layout()
plt.show()
Leave a Comment