marketing_plan.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import pandas as pd
  2. import numpy as np
  3. from pathlib import Path
  4. import json
  5. base_dir = Path(__file__).parent.parent
  6. filename = base_dir / "data" / "Marketingplanung_AHR_2023_V1.xls"
  7. output = base_dir / "export" / "marketing_2023.json"
  8. output_csv = base_dir / "export" / "marketing_2023.csv"
  9. debug_csv = base_dir / "export" / "marketing_2023_rest.csv"
  10. department = {" MM": "10", " ULM": "40", " LL": "50", "KRU": "30", "GZ": "55", "AAM": "82", "AAM-MOT": "81"}
  11. columns = [
  12. "Bezeichnung",
  13. "Segment",
  14. "Termin",
  15. "Wert1",
  16. "Wert2",
  17. "Wert3",
  18. "Wert4",
  19. "Wert5",
  20. "Wert6",
  21. "Wert7",
  22. "Wert8",
  23. "Wert9",
  24. "Wert10",
  25. "Wert11",
  26. "Wert12",
  27. "Summe",
  28. ]
  29. defaults = ["", "", "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  30. types = [str] * 3 # + [float] * 13
  31. xls = pd.read_excel(
  32. filename,
  33. sheet_name=list(department.keys()),
  34. skiprows=4,
  35. usecols="A:P",
  36. names=columns,
  37. converters=dict(zip(columns, types)),
  38. )
  39. xls_temp = []
  40. for sheet, dept_no in department.items():
  41. xls[sheet]["Betrieb Nr"] = dept_no
  42. xls[sheet] = xls[sheet].fillna(value=dict(zip(columns, defaults)))
  43. xls[sheet]["Konto Nr"] = np.where(
  44. xls[sheet]["Segment"].str.startswith("Kto "), xls[sheet]["Segment"].str.slice(4, 9), "Rest"
  45. )
  46. # xls[sheet]['Konto Nr'] = np.where(xls[sheet]['Segment'].str.startswith('50% '),
  47. # xls[sheet]['Segment'].str.slice(-5), xls[sheet]['Konto Nr'])
  48. temp = xls[sheet][xls[sheet]["Segment"].str.startswith("50% ")].copy()
  49. temp["Konto Nr"] = temp["Segment"].str.slice(8, 13)
  50. xls_temp.append(temp)
  51. temp2 = temp.copy()
  52. temp2["Konto Nr"] = temp2["Segment"].str.slice(-5)
  53. xls_temp.append(temp2)
  54. xls_temp_concat = pd.concat(xls_temp)
  55. for c in columns[3:]:
  56. xls_temp_concat[c] = xls_temp_concat[c] * 0.5
  57. df = pd.concat(list(xls.values()) + [xls_temp_concat])
  58. df_rest = df[(df["Konto Nr"] == "Rest") & (df["Summe"] != 0)]
  59. df_rest.to_csv(debug_csv, sep=";", decimal=",")
  60. df = df[df["Konto Nr"] != "Rest"]
  61. df["Wert11"] = df["Wert11"].astype(float)
  62. df["Wert12"] = df["Wert12"].astype(float)
  63. for c in columns[3:]:
  64. df[c] = df[c].round()
  65. df = df.drop(labels=["Summe"], axis=1)
  66. df = df.groupby(["Konto Nr", "Betrieb Nr"]).sum()
  67. print(df.info())
  68. # df.reset_index(inplace=True)
  69. df.to_csv(output_csv, sep=";", decimal=",")
  70. dict_split = df.to_dict(orient="split")
  71. res = {}
  72. for index, data in zip(dict_split["index"], dict_split["data"]):
  73. kto, dept = index
  74. if kto not in res:
  75. res[kto] = {}
  76. res[kto][dept] = data
  77. json.dump(res, open(output, "w"), indent=2)