pandas 按照特定順序輸出的實(shí)現(xiàn)代碼
df.groupby() 之后按照特定順序輸出,方便后續(xù)作圖,或者跟其他df對比作圖。
## 構(gòu)造 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)
## 聚類計(jì)數(shù)
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');

總結(jié)
以上所述是小編給大家介紹的pandas 按照特定順序輸出的實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Python接口自動化之淺析requests模塊post請求
這篇文章Python接口自動化之淺析requests模塊post請求,以下主要介紹requests模塊中的post請求的使用,post源碼,data、json參數(shù)應(yīng)用場景及實(shí)戰(zhàn)2021-08-08
Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法
這篇文章主要介紹了Python(TensorFlow框架)實(shí)現(xiàn)手寫數(shù)字識別系統(tǒng)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Python密碼學(xué)Caesar?Cipher凱撒密碼算法教程
這篇文章主要為大家介紹了Python密碼學(xué)Caesar?Cipher凱撒密碼算法教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
利于python腳本編寫可視化nmap和masscan的方法
這篇文章主要介紹了利于python腳本編寫可視化nmap和masscan的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
python判斷數(shù)字是否是超級素?cái)?shù)冪
這篇文章主要為大家詳細(xì)介紹了python判斷數(shù)字是否是超級素?cái)?shù)冪,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Python設(shè)計(jì)模式結(jié)構(gòu)型享元模式
這篇文章主要介紹了Python享元模式,享元模式即Flyweight Pattern,指運(yùn)用共享技術(shù)有效地支持大量細(xì)粒度的對象,下面和小編一起進(jìn)入文章了解更多詳細(xì)內(nèi)容吧2022-02-02
python中的hashlib和base64加密模塊使用實(shí)例
這篇文章主要介紹了python中的hashlib和base64加密模塊使用實(shí)例,hashlib模塊支持的加密算法有md5 sha1 sha224 sha256 sha384 sha512,需要的朋友可以參考下2014-09-09

