您好,登錄后才能下訂單哦!
df.groupby() 之后按照特定順序輸出,方便后續作圖,或者跟其他df對比作圖。
## 構造 pd.DataFrame patient_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() ## output week friday 2 monday 3 saturday 1 thursday 2 tuesday 4 wednesday 3 Name: patient_id, dtype: int64 ## 按照特定順序輸出 ind = ['monday','tuesday','wednesday','thursday','friday','saturday'] test.groupby('week')['patient_id'].count()[ind] ## output week monday 3 tuesday 4 wednesday 3 thursday 2 friday 2 saturday 1 Name: 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 按照特定順序輸出的實現代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。