Domain: Science, Environment & Research

I want to understand how farmers from my company used the fertilizers and if there interventions I can employ from my
side. Please using the data in `Strategy_Operations_March_2022.xlsx`. The OAFID is not unique for each farmer, however,
it’s unique for each district.

- There might be farmers had a positive recommended quantity for at least one fertilizer but used zero for that type.
  For each fertilizer type, find the district with the highest number of such farmers and that value. in this format:
  `district:fertilizer type:total of farmers`

- For each fertilizer type, which district–gender combination has the highest usage ratio (average used / average
  recommended). Report the district, gender, ratio for highest and lowest (ratios rounded to
  2 decimals) in this exact format: `district,gender(F or M),ratio`

- Among farmers who used more than recommended: for each fertilizer type, how many
  such farmers are there in total? Which districts has the most such users? Report the district name and values
  for each fertilizer type. For each type, report like this; `District:Fertilizer Type: Total Count in that district`

All numbers should be rounded to 2 decimals unless otherwise is specified

Grading rubric:
criterion: For farmers with positive DAP recommended but used zero is exactly Rusizi:DAP:11
weight: 20
criterion: For farmers with positive NPK recommended but used zero is exactly Nyamagabe:NPK:13
weight: 20
criterion: For farmers with positive UREA recommended but used zero is exactly Karongi:UREA:107
weight: 20
criterion: For DAP, the district-gender with the highest usage ratio is exactly Kayonza,F,4.16
weight: 10
criterion: For NPK, the district-gender with the highest usage ratio is exactly Nyamasheke,M,27.5
weight: 10
criterion: For UREA, the district-gender with the highest usage ratio is exactly Nyamagabe,M,1.68
weight: 10
criterion: Among farmers who used more than recommended for DAP, the total count is 728
weight: 10
criterion: Among farmers who used more than recommended for DAP, the district with the most such users response is reported as Karongi:DAP:164
weight: 10
criterion: Among farmers who used more than recommended for NPK, the total count is 57
weight: 10
criterion: Among farmers who used more than recommended for NPK, the district with the most such users response is reported as Karongi:NPK:19
weight: 10
criterion: Among farmers who used more than recommended for UREA, the total count is 230
weight: 10
criterion: Among farmers who used more than recommended for UREA, the district with the most such users response is reported as Rusizi:UREA:59
weight: 10

Golden answer:
```python
import pandas as pd
import numpy as np

path = "Strategy_Operations_March_2022.xlsx"

# Load sheets
fert = pd.read_excel(path, sheet_name="Fert used", skiprows=1)
are = pd.read_excel(path, sheet_name="Are data")
interp = pd.read_excel(path, sheet_name="Interpretation")

# Load recommendation table explicitly
rec = pd.read_excel(path, sheet_name="Recommended input quantities")

# Clean structure
rec = rec.rename(columns={"Unnamed: 1": "fertilizer"})
rec = rec[["fertilizer", "Maize", "Beans", "Potatoes"]].copy()
rec["fertilizer"] = rec["fertilizer"].astype(str).str.strip().str.upper()

# Build coefficient map from sheet (no hardcoding)
coef = rec.set_index("fertilizer")[["Maize", "Beans", "Potatoes"]].to_dict("index")

# Data cleaning / harmonizing
for df in (fert, are):
    df["District"] = df["District"].astype(str).str.strip().str.title()

district_fix = {
    "Bugarama": "Rusizi",
    "Giheke": "Rusizi",
    "Mugonero": "Karongi",
    "Rubengera": "Karongi",
    "Kibogora": "Nyamasheke",
}
for df in (fert, are):
    df["District"] = df["District"].replace(district_fix)

fert["Gender"] = (
    fert["Gender"]
    .astype(str)
    .str.strip()
    .str.upper()
    .replace({"MALE": "M", "FEMALE": "F"})
)

for df in (fert, are):
    df["OAFID_norm"] = pd.to_numeric(df["OAFID"], errors="coerce")

used_cols = {
    "DAP": "Total DAP (KGs)",
    "NPK": "Total NPK (KGs)",
    "UREA": "Total UREA (KGs)",
}
are_cols = [
    "Fertilized Are - maize",
    "Fertilized Are - Beans",
    "Fertilized Are - Potatoes",
]

for c in used_cols.values():
    fert[c] = pd.to_numeric(fert[c], errors="coerce").fillna(0)

for c in are_cols:
    are[c] = pd.to_numeric(are[c], errors="coerce").fillna(0)

# Merge + recommended amounts
m = fert.merge(
    are[["District", "OAFID_norm"] + are_cols],
    on=["District", "OAFID_norm"],
    how="left",
    validate="1:1",
)

# Recommended amounts per farmer based on Are data
m["rec_DAP"] = (
        coef["DAP"]["Maize"] * m["Fertilized Are - maize"]
        + coef["DAP"]["Beans"] * m["Fertilized Are - Beans"]
        + coef["DAP"]["Potatoes"] * m["Fertilized Are - Potatoes"]
)

m["rec_UREA"] = (
        coef["UREA"]["Maize"] * m["Fertilized Are - maize"]
        + coef["UREA"]["Beans"] * m["Fertilized Are - Beans"]
        + coef["UREA"]["Potatoes"] * m["Fertilized Are - Potatoes"]
)

m["rec_NPK"] = (
        coef["NPK"]["Maize"] * m["Fertilized Are - maize"]
        + coef["NPK"]["Beans"] * m["Fertilized Are - Beans"]
        + coef["NPK"]["Potatoes"] * m["Fertilized Are - Potatoes"]
)

# Per farmer per fertilizer %
rows = []
for fert_type, used_col in used_cols.items():
    rec_col = f"rec_{fert_type}"
    tmp = m[["District", "OAFID_norm", "Gender", used_col, rec_col]].copy()
    tmp = tmp.rename(columns={used_col: "A_used", rec_col: "B_rec"})
    tmp["fertilizer_type"] = fert_type
    tmp["pct_used_vs_rec"] = np.where(
        tmp["B_rec"] > 0, 100 * tmp["A_used"] / tmp["B_rec"], np.nan
    )
    rows.append(tmp)

long = pd.concat(rows, ignore_index=True)

fert_used_pct = long.pivot_table(
    index=["District", "OAFID_norm", "Gender"],
    columns="fertilizer_type",
    values="pct_used_vs_rec",
    aggfunc="first",
).reset_index()
fert_used_pct.columns = [
    (
        "District"
        if c == "District"
        else (
            "OAFID_norm"
            if c == "OAFID_norm"
            else "Gender" if c == "Gender" else f"pct_{str(c).lower()}"
        )
    )
    for c in fert_used_pct.columns
]

valid = long[long["B_rec"] > 0].copy()

# Summary table: district x gender x fertilizer (for usage ratio)
summary = long.groupby(["District", "Gender", "fertilizer_type"], as_index=False).agg(
    avg_used_per_farmer_A=("A_used", "mean"),
    avg_recommended_per_farmer_B=("B_rec", "mean"),
)
summary["ratio"] = np.where(
    summary["avg_recommended_per_farmer_B"] > 0,
    summary["avg_used_per_farmer_A"] / summary["avg_recommended_per_farmer_B"],
    np.nan,
)

# Gender overall and per-fertilizer
gender_tot = valid.groupby("Gender", as_index=False).agg(
    total_used=("A_used", "sum"), total_rec=("B_rec", "sum")
)
gender_tot["overall_pct"] = 100 * gender_tot["total_used"] / gender_tot["total_rec"]

gender_fert = valid.groupby(["Gender", "fertilizer_type"], as_index=False).agg(
    total_used=("A_used", "sum"), total_rec=("B_rec", "sum")
)
gender_fert["pct_of_recommended"] = (
        100 * gender_fert["total_used"] / gender_fert["total_rec"]
)

# More compliant sex: which fertilizer most/least used vs recommendation
more_compliant = gender_tot.loc[gender_tot["overall_pct"].idxmax(), "Gender"]
f_fert = gender_fert[gender_fert["Gender"] == more_compliant].sort_values(
    "pct_of_recommended", ascending=False
)

# Farmers with B_rec > 0 but A_used == 0
zero_used = long[(long["B_rec"] > 0) & (long["A_used"] == 0)].copy()
farmers_zero_any = zero_used.drop_duplicates(subset=["District", "OAFID_norm"])
per_type_district = (
    zero_used.groupby(["fertilizer_type", "District"], as_index=False)["OAFID_norm"]
    .nunique()
    .rename(columns={"OAFID_norm": "count"})
)
top_zero_district = per_type_district.loc[
    per_type_district.groupby("fertilizer_type")["count"].idxmax()
]
print("\n Farmers with positive recommended but used zero")
print(f"  Total such farmers: {len(farmers_zero_any)}")
for _, r in top_zero_district.iterrows():
    # format: District:Fertilizer:count
    print(f"  {r['District']}:{r['fertilizer_type']}:{int(r['count'])}")

# Per fertilizer type – district–gender highest usage ratio (format: District,Gender,ratio)
print("\nDistrict-gender highest usage ratio per fertilizer type")
for ftype in ["DAP", "NPK", "UREA"]:
    sub = summary[summary["fertilizer_type"] == ftype].dropna(subset=["ratio"])
    if sub.empty:
        continue
    high = sub.loc[sub["ratio"].idxmax()]
    # format: District,Gender,ratio (e.g. Kayonza,F,4.16)
    print(f"  {ftype}: {high['District']},{high['Gender']},{high['ratio']:.2f}")

# Farmers who used more than recommended
over_used = long[(long["B_rec"] > 0) & (long["A_used"] > long["B_rec"])].copy()
over_per_type = (
    over_used.groupby("fertilizer_type", as_index=False)["OAFID_norm"]
    .nunique()
    .rename(columns={"OAFID_norm": "total"})
)
over_per_district = (
    over_used.groupby(["fertilizer_type", "District"], as_index=False)["OAFID_norm"]
    .nunique()
    .rename(columns={"OAFID_norm": "count"})
)
top_over_district = over_per_district.loc[
    over_per_district.groupby("fertilizer_type")["count"].idxmax()
]
print("\n Farmers who used more than recommended")
for ftype in ["DAP", "NPK", "UREA"]:
    tot = over_per_type[over_per_type["fertilizer_type"] == ftype]["total"].iloc[0]
    row = top_over_district[top_over_district["fertilizer_type"] == ftype].iloc[0]
    # format: total count as number; district with most as District:Fertilizer:count
    print(f"  {ftype}: total = {int(tot)}")
    print(f"  {ftype}: {row['District']}:{ftype}:{int(row['count'])}")

```

**Output**


 Farmers with positive recommended but used zero
  Total such farmers: 684
  Rusizi:DAP:11
  Nyamagabe:NPK:13
  Karongi:UREA:107

District-gender highest usage ratio per fertilizer type
  DAP: Kayonza,F,4.16
  NPK: Nyamasheke,M,27.50
  UREA: Nyamagabe,M,1.68

 Farmers who used more than recommended
  DAP: total = 728
  DAP: Karongi:DAP:164
  NPK: total = 57
  NPK: Karongi:NPK:19
  UREA: total = 230
  UREA: Rusizi:UREA:59