栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

您可以格式化要显示的pandas整数,例如用于浮动的`pd.options.display.float_format`吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

您可以格式化要显示的pandas整数,例如用于浮动的`pd.options.display.float_format`吗?

您可以猴子补丁

pandas.io.formats.format.IntArrayFormatter

import contextlibimport numpy as npimport pandas as pdimport pandas.io.formats.format as pfnp.random.seed(2015)@contextlib.contextmanagerdef custom_formatting():    orig_float_format = pd.options.display.float_format    orig_int_format = pf.IntArrayFormatter    pd.options.display.float_format = '{:0,.2f}'.format    class IntArrayFormatter(pf.GenericArrayFormatter):        def _format_strings(self): formatter = self.formatter or '{:,d}'.format fmt_values = [formatter(x) for x in self.values] return fmt_values    pf.IntArrayFormatter = IntArrayFormatter    yield    pd.options.display.float_format = orig_float_format    pf.IntArrayFormatter = orig_int_formatdf = pd.Dataframe(np.random.randint(10000, size=(5,3)), columns=list('ABC'))df['D'] = np.random.random(df.shape[0])*10000with custom_formatting():    print(df)

产量

      A     B     C        D0 2,658 2,828 4,540 8,961.771 9,506 2,734 9,805 2,221.862 3,765 4,152 4,583 2,011.823 5,244 5,395 7,485 8,656.084 9,107 6,033 5,998 2,942.53

在之外时

with-statement

print(df)

产量

      A     B     C D0  2658  2828  4540  8961.7652601  9506  2734  9805  2221.8647792  3765  4152  4583  2011.8237013  5244  5395  7485  8656.0756104  9107  6033  5998  2942.530551


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/429171.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号