我是编程新手,感谢你的帮助。试图避免重复查询熊猫数据的代码。
x1是带有各种列名的数据格式,如高血压、糖尿病、酒精中毒、残疾、Age_Group、Date_Appointment
上面列出的每个疾病列都包含0-没有疾病,2/3/4 -有不同的疾病阶段。
所以当我在“!= 0”上过滤时,它会列出患有这种特定疾病的病人的记录。因此,每种疾病都会过滤出不同的记录。
我在下面的查询中写了4遍,用其他疾病替换了“高血压”一词,得到了每种疾病的4种不同的图表。
但这不是干净的编码。我需要帮助了解如何使用哪个函数,以及如何使用它只编写1个查询而不是4个查询。
hyp1 = x1.query('Hypertension != 0')
i1 = hyp1.groupby('Age_Group')['Hypertension'].value_counts().plot(kind = 'bar',label = 'Hypertension',figsize=(6, 6))
plt.title('Appointments Missed by Patients with Hypertension')
plt.xlabel('Hypertension Age_Group')
plt.ylabel('Appointments missed');下面是另一套,我不知道如何压缩。
`print('Details of all appointments')
`print('')`
`print(df.Date_Appointment.value_counts().sort_index())`
`print('')`
`print(df.Date_Appointment.describe())`
`print('')`
`print(df.Date_Appointment.value_counts().describe())`
`print('')`
`print('Median = ', (round(df.Date_Appointment.value_counts().mean())))`
`print('Median = ', (round (df.Date_Appointment.value_counts().median())))`
`print('Mode = ', (df.Date_Appointment.value_counts().mode()))`非常感谢你的详细答复。提前谢谢你。
发布于 2020-04-21 04:19:20
columns
f'{...}))。
diseases = {'Hypertension': 'red', 'Diabetes': 'blue', 'Alcoholism': 'green', 'Handicap': 'yellow'}
for disease, color in diseases.items():
subset = x1.query(f'{disease} != 0')
i1 = subset.groupby('Age_Group')[f'{disease}'].value_counts().plot(kind='bar', label=f'{disease}', figsize=(6, 6), color=color)
plt.title(f'Appointments Missed by Patients with {disease}')
plt.xlabel(f'{disease} Age Group')
plt.ylabel('Appointments missed')
plt.show()来压缩或替换Date_Appointment
https://stackoverflow.com/questions/61335399
复制相似问题