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()。
Syntax: numpy.round_(arr, decimals = 0, out = None)
Return: An array with all array elements being
rounded off, having same type as input.
此方法可用于将任何特定列的值四舍五入到特定的小数位,也可用于将整个数据框的值四舍五入到特定的小数位数。
示例:将“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()。
Syntax: Dataframe/Series.apply(func, convert_dtype=True, args=())
Return: Pandas Series after applied function/operation.
Syntax: numpy.ceil(x[, out]) = ufunc ‘ceil’)
Return: An array with the ceil of each element of float data-type.
这些方法用于将值舍入到最大值(大于特定值的最小整数值)。
示例:对特定列的值进行四舍五入。
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
输出:
方法 3:同时使用 Dataframe.apply() 和 numpy.floor()。
Syntax: numpy.floor(x[, out]) = ufunc ‘floor’)
Return: An array with the floor of each element.
这些方法用于将值四舍五入为底值(小于特定值的最大整数值)。
示例:将“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
输出: