Pandas格式化DataFrame的浮點(diǎn)數(shù)列的實(shí)現(xiàn)
在呈現(xiàn)數(shù)據(jù)的同時(shí),以所需的格式顯示數(shù)據(jù)也是一個(gè)重要而關(guān)鍵的部分。有時(shí),值太大了,我們只想顯示其中所需的部分,或者我們可以說(shuō)以某種所需的格式。
讓我們看看在Pandas中格式化DataFrame的數(shù)值列的不同方法。
例1:將列值四舍五入到兩位小數(shù)
# import pandas lib as pd import pandas as pd # create the data dictionary data = {'Month' : ['January', 'February', 'March', 'April'], 'Expense': [ 21525220.653, 31125840.875, 23135428.768, 56245263.942]} # create the dataframe dataframe = pd.DataFrame(data, columns = ['Month', 'Expense']) print("Given Dataframe :\n", dataframe) # round to two decimal places in python pandas pd.options.display.float_format = '{:.2f}'.format print('\nResult :\n', dataframe)
例2:用逗號(hào)格式化整數(shù)列,并四舍五入到兩位小數(shù)
# import pandas lib as pd import pandas as pd # create the data dictionary data = {'Month' : ['January', 'February', 'March', 'April'], 'Expense':[ 21525220.653, 31125840.875, 23135428.768, 56245263.942]} # create the dataframe dataframe = pd.DataFrame(data, columns = ['Month', 'Expense']) print("Given Dataframe :\n", dataframe) # Format with commas and round off to two decimal places in pandas pd.options.display.float_format = '{:, .2f}'.format print('\nResult :\n', dataframe)
例3:格式劃列與逗號(hào)和$符號(hào),并四舍五入到兩位小數(shù)
# import pandas lib as pd import pandas as pd # create the data dictionary data = {'Month' : ['January', 'February', 'March', 'April'], 'Expense':[ 21525220.653, 31125840.875, 23135428.768, 56245263.942]} # create the dataframe dataframe = pd.DataFrame(data, columns = ['Month', 'Expense']) print("Given Dataframe :\n", dataframe) # Format with dollars, commas and round off # to two decimal places in pandas pd.options.display.float_format = '${:, .2f}'.format print('\nResult :\n', dataframe)
到此這篇關(guān)于Pandas格式化DataFrame的浮點(diǎn)數(shù)列的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Pandas DataFrame浮點(diǎn)數(shù)列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)復(fù)雜對(duì)象轉(zhuǎn)JSON的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)復(fù)雜對(duì)象轉(zhuǎn)JSON的方法,結(jié)合具體實(shí)例形式分析了Python針對(duì)json轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06python實(shí)現(xiàn)停車管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)停車管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Python 中如何使用 setLevel() 設(shè)置日志級(jí)別
這篇文章主要介紹了在 Python 中使用setLevel() 設(shè)置日志級(jí)別,Python 提供了一個(gè)單獨(dú)的日志記錄模塊作為其標(biāo)準(zhǔn)庫(kù)的一部分,以簡(jiǎn)化日志記錄,本文將討論日志記錄 setLevel 及其在 Python 中的工作方式,需要的朋友可以參考下2023-07-07在Python中,不用while和for循環(huán)遍歷列表的實(shí)例
今天小編就為大家分享一篇在Python中,不用while和for循環(huán)遍歷列表的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02python將excel轉(zhuǎn)換為csv的代碼方法總結(jié)
在本篇文章里小編給大家分享了關(guān)于python如何將excel轉(zhuǎn)換為csv的實(shí)例方法和代碼內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-07-07Python網(wǎng)絡(luò)編程之xmlrpc模塊
這篇文章介紹了Python網(wǎng)絡(luò)編程之xmlrpc模塊,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05