如何删除pandas中的小数点

时间:2016-05-07 05:21:51

标签: python pandas

我有一个pandas数据框,df,如下所示:

Cut-off             <=35   >35                   
Calcium              0.0   1.0
Copper               1.0   0.0
Helium               0.0   8.0
Hydrogen             0.0   1.0

如何删除小数点,使数据框如下所示:

Cut-off             <= 35  > 35                   
Calcium              0     1
Copper               1     0
Helium               0     8
Hydrogen             0     1

我试过df.round(0)但没有成功。

3 个答案:

答案 0 :(得分:25)

你有几个选择......

1)将所有内容转换为整数。

df.astype(int)
          <=35  >35
Cut-off            
Calcium      0    1
Copper       1    0
Helium       0    8
Hydrogen     0    1

2)使用round

>>> df.round()
          <=35  >35
Cut-off            
Calcium      0    1
Copper       1    0
Helium       0    8
Hydrogen     0    1

但并不总是很棒...

>>> (df - .2).round()
          <=35  >35
Cut-off            
Calcium     -0    1
Copper       1   -0
Helium      -0    8
Hydrogen    -0    1

3)更改Pandas中的显示精度选项。

pd.set_option('precision', 0)

>>> df
          <=35  >35
Cut-off            
Calcium      0    1
Copper       1    0
Helium       0    8
Hydrogen     0    1 

答案 1 :(得分:4)

由于pandas 0.17.1,您可以将显示的数值精度设置为modifying the style of the particular data frame,而不是设置全局选项:

import pandas as pd
import numpy as np

np.random.seed(24)
df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))
df 

enter image description here

df.style.set_precision(2)

enter image description here

也可以应用列特定样式

df.style.format({
    'A': '{:,.1f}'.format,
    'B': '{:,.3f}'.format,
})

enter image description here

答案 2 :(得分:1)

如果您根本不需要小数,也可以使用此代码:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:3000/{R:1}" />
                </rule>
            </rules>
            <outboundRules>

                <rule name="testing2" preCondition="IsHTML" stopProcessing="true">
                    <match filterByTags="Img, Link, Script" pattern="(.*)(images|styles|scripts)(.*)" />
                    <action type="Rewrite" value="{R:1}api/{R:2}{R:3}" />
                </rule> 
                <preConditions>
                    <preCondition name="IsHTML">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>