数据预处理

df = df.dropna(subset=['age', 'marital']) df['marital'] = df['marital'].replace({'single': '未婚', 'married': '已婚', 'divorced': '离婚'}) df = df.groupby(['age', 'marital'])['marital'].count().reset_index(name='count') age_list = df['age'].unique().tolist() marital_list = df['marital'].unique().tolist() counts = [] for marital in marital_list: count = [] for age in age_list: if df[(df['age'] == age) & (df['marital'] == marital)].empty: count.append(0) else: count.append(df[(df['age'] == age) & (df['marital'] == marital)]['count'].values[0]) counts.append(count)

可视化

output_file('age_marital.html') p = figure(x_range=age_list, plot_height=400, title='不同年龄段已婚/未婚/离婚人数', sizing_mode='stretch_width') colors = ['#c9d9d3', '#718dbf', '#e84d60'] for i, marital in enumerate(marital_list): p.vbar(x=age_list, top=counts[i], width=0.9, color=colors[i], legend_label=marital) p.xgrid.grid_line_color = None p.legend.orientation = 'horizontal' p.legend.location = 'top_center' show(p


原文地址: http://www.cveoy.top/t/topic/e9ZW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录