Pandas格式化DataFrame的浮点数列的实现
作者:python收藏家
本文主要介绍了Pandas格式化DataFrame的浮点数列的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在呈现数据的同时,以所需的格式显示数据也是一个重要而关键的部分。有时,值太大了,我们只想显示其中所需的部分,或者我们可以说以某种所需的格式。
让我们看看在Pandas中格式化DataFrame的数值列的不同方法。
例1:将列值四舍五入到两位小数
# 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:用逗号格式化整数列,并四舍五入到两位小数
# 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:格式划列与逗号和$符号,并四舍五入到两位小数
# 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的浮点数列的实现的文章就介绍到这了,更多相关Pandas DataFrame浮点数列内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!