As the prognosis of cancer patients has been improved, comorbidity of heart failure (HF) in cancer survivors is a serious concern, especially in the aged population. This study aimed to examine the risk factors of HF development after treatment by anticancer agents, using a machine learning-based analysis of a massive dataset obtained from the electronic health record (EHR) in Japan. This retrospective, cohort study, using a dataset from 2008 to 2017 in the Diagnosis Procedure Combination (DPC) database in Japan, enrolled 140,327 patients. The structure of risk factors was determined using multivariable analysis and classification and regression tree (CART) algorithm for time-to-event data. The mean follow-up period was 1.55 years. The prevalence of HF after anticancer agent administration were 4.0%. HF was more prevalent in the older than the younger. As the presence of cardiovascular diseases and various risk factors predicted HF, CART analysis of the risk factors revealed that the risk factor structures complicatedly differed among different age groups. The highest risk combination was hypertension, diabetes mellitus, and atrial fibrillation in the group aged  ≤ 64 years, and the presence of ischemic heart disease was a key in both groups aged 65–74 years and 75 ≤ years. The machine learning-based approach was able to develop complicated HF risk structures in cancer patients after anticancer agents in different age population, of which knowledge would be essential for realizing precision medicine to improve the prognosis of cancer patients.Supplementary InformationThe online version contains supplementary material available at 10.1007/s00380-023-02238-9.