Pandas格式化DataFrame的浮點數(shù)列的實現(xiàn)
在呈現(xiàn)數(shù)據(jù)的同時,以所需的格式顯示數(shù)據(jù)也是一個重要而關鍵的部分。有時,值太大了,我們只想顯示其中所需的部分,或者我們可以說以某種所需的格式。
讓我們看看在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:用逗號格式化整數(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:格式劃列與逗號和$符號,并四舍五入到兩位小數(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) 
到此這篇關于Pandas格式化DataFrame的浮點數(shù)列的實現(xiàn)的文章就介紹到這了,更多相關Pandas DataFrame浮點數(shù)列內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python使用python-docx實現(xiàn)自動化處理Word文檔
這篇文章主要為大家展示了Python如何通過代碼實現(xiàn)段落樣式復制,HTML表格轉Word表格以及動態(tài)生成可定制化模板的功能,感興趣的小伙伴可以跟隨小編一起學習一下2025-05-05
使用python讀取csv文件快速插入數(shù)據(jù)庫的實例
今天小編就為大家分享一篇使用python讀取csv文件快速插入數(shù)據(jù)庫的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
Python依賴管理神器requirements.txt從安裝到實戰(zhàn)完全指南
requirements.txt是 Python 項目中用于統(tǒng)一管理庫包版本的純文本文件,它記錄了項目依賴的所有庫包及其精確版本(或版本范圍),這篇文章主要介紹了Python依賴管理神器requirements.txt從安裝到實戰(zhàn)的相關資料,需要的朋友可以參考下2026-05-05
pip install -e.出現(xiàn)xxx module not fou
這篇文章主要介紹了pip install -e.出現(xiàn)xxx module not found error的問題解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-07-07

