fork download
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3.  
  4. # Data input
  5. hic_data = {
  6. "Country": ["Spain", "Norway", "Luxembourg", "Switzerland", "Ireland"],
  7. "Mean Age at First Birth": [31.5, 30.1, 31.4, 31.3, 31.6]
  8. }
  9.  
  10. lic_mic_data = {
  11. "Country": ["Rwanda", "Algeria", "Ukraine", "Philippines", "Iran"],
  12. "Mean Age at First Birth": [22.5, 24.5, 24.9, 22.3, 27.4]
  13. }
  14.  
  15. # Convert to DataFrames
  16. hic_df = pd.DataFrame(hic_data)
  17. lic_mic_df = pd.DataFrame(lic_mic_data)
  18.  
  19. # Calculate overall means
  20. hic_mean = hic_df["Mean Age at First Birth"].mean()
  21. lic_mic_mean = lic_mic_df["Mean Age at First Birth"].mean()
  22.  
  23. # Combine for display
  24. summary_df = pd.DataFrame({
  25. "Group": ["High-Income Countries (HICs)", "Low- and Middle-Income Countries (LICs/MICs)"],
  26. "Overall Mean Age at First Birth": [hic_mean, lic_mic_mean]
  27. })
  28.  
  29. # Plot chart
  30. plt.figure(figsize=(10,6))
  31. plt.bar(hic_df["Country"], hic_df["Mean Age at First Birth"], label="HICs", alpha=0.7)
  32. plt.bar(lic_mic_df["Country"], lic_mic_df["Mean Age at First Birth"], label="LICs/MICs", alpha=0.7)
  33. plt.axhline(hic_mean, color='blue', linestyle='--', linewidth=1.2, label=f"HIC Mean: {hic_mean:.1f}")
  34. plt.axhline(lic_mic_mean, color='orange', linestyle='--', linewidth=1.2, label=f"LIC/MIC Mean: {lic_mic_mean:.1f}")
  35. plt.title("Mean Age at First Birth by Country", fontsize=14)
  36. plt.ylabel("Mean Age at First Birth (years)", fontsize=12)
  37. plt.xticks(rotation=45)
  38. plt.legend()
  39. plt.tight_layout()
  40.  
  41. import caas_jupyter_tools
  42. caas_jupyter_tools.display_dataframe_to_user("Summary of Overall Mean Ages", summary_df)
  43.  
  44. plt.show()
  45.  
Success #stdin #stdout 0.03s 25832KB
stdin
Standard input is empty
stdout
import pandas as pd
import matplotlib.pyplot as plt

# Data input
hic_data = {
    "Country": ["Spain", "Norway", "Luxembourg", "Switzerland", "Ireland"],
    "Mean Age at First Birth": [31.5, 30.1, 31.4, 31.3, 31.6]
}

lic_mic_data = {
    "Country": ["Rwanda", "Algeria", "Ukraine", "Philippines", "Iran"],
    "Mean Age at First Birth": [22.5, 24.5, 24.9, 22.3, 27.4]
}

# Convert to DataFrames
hic_df = pd.DataFrame(hic_data)
lic_mic_df = pd.DataFrame(lic_mic_data)

# Calculate overall means
hic_mean = hic_df["Mean Age at First Birth"].mean()
lic_mic_mean = lic_mic_df["Mean Age at First Birth"].mean()

# Combine for display
summary_df = pd.DataFrame({
    "Group": ["High-Income Countries (HICs)", "Low- and Middle-Income Countries (LICs/MICs)"],
    "Overall Mean Age at First Birth": [hic_mean, lic_mic_mean]
})

# Plot chart
plt.figure(figsize=(10,6))
plt.bar(hic_df["Country"], hic_df["Mean Age at First Birth"], label="HICs", alpha=0.7)
plt.bar(lic_mic_df["Country"], lic_mic_df["Mean Age at First Birth"], label="LICs/MICs", alpha=0.7)
plt.axhline(hic_mean, color='blue', linestyle='--', linewidth=1.2, label=f"HIC Mean: {hic_mean:.1f}")
plt.axhline(lic_mic_mean, color='orange', linestyle='--', linewidth=1.2, label=f"LIC/MIC Mean: {lic_mic_mean:.1f}")
plt.title("Mean Age at First Birth by Country", fontsize=14)
plt.ylabel("Mean Age at First Birth (years)", fontsize=12)
plt.xticks(rotation=45)
plt.legend()
plt.tight_layout()

import caas_jupyter_tools
caas_jupyter_tools.display_dataframe_to_user("Summary of Overall Mean Ages", summary_df)

plt.show()