首页 > 编程 > Python > 正文

pandas 按照特定顺序输出的实现代码

2020-02-15 22:21:27
字体:
来源:转载
供稿:网友

df.groupby() 之后按照特定顺序输出,方便后续作图,或者跟其他df对比作图。

## 构造 pd.DataFramepatient_id = ['71835318256532', '87791375711', '66979212649388', '46569922967175', '998612492555522', '982293214194', '89981833848', '17912315786975', '4683495482494', '1484143378533', '56866972273357', '7796319285658', '414462476158336', '449519578512573', '61826664459895']week = ['tuesday', 'tuesday', 'wednesday', 'monday', 'tuesday', 'monday', 'friday', 'tuesday', 'monday', 'friday', 'saturday', 'thursday', 'wednesday', 'thursday', 'wednesday']d = {'patient_id': patient_id, 'week':week}test = pd.DataFrame(data=d)## 聚类计数test.groupby('week')['patient_id'].count()## outputweekfriday  2monday  3saturday  1thursday  2tuesday  4wednesday 3Name: patient_id, dtype: int64## 按照特定顺序输出ind = ['monday','tuesday','wednesday','thursday','friday','saturday']test.groupby('week')['patient_id'].count()[ind]## outputweekmonday  3tuesday  4wednesday 3thursday  2friday  2saturday  1Name: patient_id, dtype: int64

作图效果如下

test.groupby('week')['patient_id'].count().plot(kind='bar');

这里写图片描述

ind = ['monday','tuesday','wednesday','thursday','friday','saturday']test.groupby('week')['patient_id'].count()[ind].plot(kind='bar');

这里写图片描述

总结

以上所述是小编给大家介绍的pandas 按照特定顺序输出的实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林站长站网站的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表