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使用python-docx實(shí)現(xiàn)自動(dòng)化處理Word文檔
這篇文章主要為大家展示了Python如何通過(guò)代碼實(shí)現(xiàn)段落樣式復(fù)制,HTML表格轉(zhuǎn)Word表格以及動(dòng)態(tài)生成可定制化模板的功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-05-05
使用python讀取csv文件快速插入數(shù)據(jù)庫(kù)的實(shí)例
今天小編就為大家分享一篇使用python讀取csv文件快速插入數(shù)據(jù)庫(kù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
解決Windows下python和pip命令無(wú)法使用的問(wèn)題
這篇文章主要介紹了解決Windows下python和pip命令無(wú)法使用的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Python依賴管理神器requirements.txt從安裝到實(shí)戰(zhàn)完全指南
requirements.txt是 Python 項(xiàng)目中用于統(tǒng)一管理庫(kù)包版本的純文本文件,它記錄了項(xiàng)目依賴的所有庫(kù)包及其精確版本(或版本范圍),這篇文章主要介紹了Python依賴管理神器requirements.txt從安裝到實(shí)戰(zhàn)的相關(guān)資料,需要的朋友可以參考下2026-05-05
pip install -e.出現(xiàn)xxx module not fou
這篇文章主要介紹了pip install -e.出現(xiàn)xxx module not found error的問(wèn)題解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-07-07
Django contenttypes 框架詳解(小結(jié))
這篇文章主要介紹了Django contenttypes 框架詳解(小結(jié)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

