pandas DataFrame 中的自定义浮点格式 [英] Customized float formatting in a pandas DataFrame

查看:69
本文介绍了pandas DataFrame 中的自定义浮点格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataFrame:

   0       1
0  3.000   5.600
1  1.200   3.456

出于演示目的,我希望将其转换为

and for presentation purposes I would like it to be converted to

   0    1
0  3    5.6
1  1.2  3.456

实现这一点的优雅方法是什么(不会在 DataFrame 的条目上进行低效循环)?

What is the elegant way to achieve this (without looping inefficiently over entries of the DataFrame)?

或者更一般地说:有没有办法设置 pandas 使其始终这样做?例如.pandas 选项之一?

Or perhaps more generally: is there a way to set pandas up such that it is always doing this? E.g. one of the pandas options?

请注意 pd.options.display.float_format = '{:,.0f}'.format 将不起作用,因为它会给出固定的小数位数,而不是让它变化我上面指出的 DataFrame 的条目.

Notice that pd.options.display.float_format = '{:,.0f}'.format will not work, as it would give a fixed number of decimals, rather than having it vary across entries of the DataFrame as I indicated above.

推荐答案

In [188]: df
Out[188]:
       a      b       c
0 1.0000 2.2460  2.0000
1 3.0000 4.4920  6.0000
2 5.0000 6.7380 10.0000

In [189]: pd.options.display.float_format = '{:,.2f}'.format

In [190]: df.apply(lambda x: x.astype(int) if np.allclose(x, x.astype(int)) else x)
Out[190]:
   a    b   c
0  1 2.25   2
1  3 4.49   6
2  5 6.74  10

更新:

In [222]: df
Out[222]:
       0      1
0 3.0000 5.6000
1 1.2000 3.4560

In [223]: df.applymap(lambda x: str(int(x)) if abs(x - int(x)) < 1e-6 else str(round(x,2)))
Out[223]:
     0     1
0    3   5.6
1  1.2  3.46

注意:请注意,.applymap() 方法非常慢,因为它为 DataFrame 中的每个系列执行 map(func, series)

NOTE: be aware that .applymap() method is pretty slow as it's doing map(func, series) for each series in the DataFrame

这篇关于pandas DataFrame 中的自定义浮点格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆