📜  Pandas DataFrame 中舍入值的方法

📅  最后修改于: 2022-05-13 01:55:05.832000             🧑  作者: Mango

Pandas DataFrame 中舍入值的方法

在 Pandas DataFrame 中有多种舍入值的方法,让我们一一查看:

让我们创建一个仅包含“数据条目”列的数据框:

代码:

Python3
# import Dataframe class
# from pandas library
from pandas import DataFrame
  
# import numpy library
import numpy as np
  
# dictionary
Myvalue = {'DATA ENTRY': [4.834327, 5.334477,
                          6.89, 7.6454, 8.9659]} 
  
# create a Dataframe
df = DataFrame(Myvalue,
               columns = ['DATA ENTRY'])
  
# show the dataframe
df


Python3
# import Dataframe class
# from pandas library
from pandas import DataFrame
  
# import numpy library
import numpy as np
  
# dictionary
Myvalue = {'DATA ENTRY': [4.834327, 5.334477,
                          6.89, 7.6454, 8.9659]} 
  
# create a Dataframe
df = DataFrame(Myvalue,
               columns = ['DATA ENTRY'])
  
# Rounding value of 'DATA ENTRY' 
# column upto 2 decimal places
roundplaces = np.round(df['DATA ENTRY'],
                       decimals = 2) 
  
# show the rounded value
roundplaces


Python3
# import Dataframe from 
# pandas library
from pandas import DataFrame
  
# import numpy
import numpy as np
  
# dictionary
Myvalue = {'DATA ENTRY': [4.834327, 5.334477,
                          6.89, 7.6454, 8.9659]} 
  
# create a Dataframe
df = DataFrame(Myvalue, 
               columns = ['DATA ENTRY'])
  
# Here we are rounding the 
# value to its ceiling values
roundUp = df['DATA ENTRY'].apply(np.ceil) 
  
# show the rounded value
roundUp


Python3
# import Dataframe class 
# from pandas library
from pandas import DataFrame
  
# import numpy library
import numpy as np
  
# dictionary
Myvalue = {'DATA ENTRY':[4.834327, 5.334477, 
                         6.89, 7.6454, 8.9659] } 
# create a Dataframe
df = DataFrame(Myvalue, 
               columns = ['DATA ENTRY']) 
  
# Rounding of Value to its Floor value 
rounddown = df['DATA ENTRY'].apply(np.floor)  
  
# show the rounded value
rounddown


输出:

数据框

方法一:使用 numpy.round()。

此方法可用于将任何特定列的值四舍五入到特定的小数位,也可用于将整个数据框的值四舍五入到特定的小数位数。

示例:将“DATA ENTRY”列的值四舍五入到小数点后 2 位。

Python3

# import Dataframe class
# from pandas library
from pandas import DataFrame
  
# import numpy library
import numpy as np
  
# dictionary
Myvalue = {'DATA ENTRY': [4.834327, 5.334477,
                          6.89, 7.6454, 8.9659]} 
  
# create a Dataframe
df = DataFrame(Myvalue,
               columns = ['DATA ENTRY'])
  
# Rounding value of 'DATA ENTRY' 
# column upto 2 decimal places
roundplaces = np.round(df['DATA ENTRY'],
                       decimals = 2) 
  
# show the rounded value
roundplaces

输出:

圆形数据框

方法二:同时使用 Dataframe.apply() 和 numpy.ceil()。

这些方法用于将值舍入到最大值(大于特定值的最小整数值)。

示例:对特定列的值进行四舍五入。

Python3

# import Dataframe from 
# pandas library
from pandas import DataFrame
  
# import numpy
import numpy as np
  
# dictionary
Myvalue = {'DATA ENTRY': [4.834327, 5.334477,
                          6.89, 7.6454, 8.9659]} 
  
# create a Dataframe
df = DataFrame(Myvalue, 
               columns = ['DATA ENTRY'])
  
# Here we are rounding the 
# value to its ceiling values
roundUp = df['DATA ENTRY'].apply(np.ceil) 
  
# show the rounded value
roundUp

输出:

圆形 Dataframe-2

方法 3:同时使用 Dataframe.apply() 和 numpy.floor()。

这些方法用于将值四舍五入为底值(小于特定值的最大整数值)。

示例:将“DATA ENTRY”列的值四舍五入到其对应的 Floor 值。

Python3

# import Dataframe class 
# from pandas library
from pandas import DataFrame
  
# import numpy library
import numpy as np
  
# dictionary
Myvalue = {'DATA ENTRY':[4.834327, 5.334477, 
                         6.89, 7.6454, 8.9659] } 
# create a Dataframe
df = DataFrame(Myvalue, 
               columns = ['DATA ENTRY']) 
  
# Rounding of Value to its Floor value 
rounddown = df['DATA ENTRY'].apply(np.floor)  
  
# show the rounded value
rounddown

输出:

圆形 Dataframe-3