Python Pandas中某一列的对数和自然对数值
pandas中某一列的对数和自然对数值可以分别用log()、log2()和log10()numpy函数来计算。在应用这些函数之前,我们需要创建一个数据框架。
代码:
# Import required libraries
import pandas as pd
import numpy as np
# Dictionary
data = {
'Name': ['Geek1', 'Geek2',
'Geek3', 'Geek4'],
'Salary': [18000, 20000,
15000, 35000]}
# Create a dataframe
data = pd.DataFrame(data,
columns = ['Name',
'Salary'])
# Show the dataframe
data
输出:
在pandas中对某一列的基2值进行Logarithm计算:
数据框架创建后,我们可以对各列应用numpy.log2()函数。在这种情况下,我们将找到工资这一列的对数值。计算出的值将被存储在新的列 “logarithm_base2 “中。
代码:
# Calculate logarithm to base 2
# on 'Salary' column
data['logarithm_base2'] = np.log2(data['Salary'])
# Show the dataframe
data
输出 :
在pandas中对一列的基数为10的数值进行Logarithm:
为了找到以10为基数的对数,我们可以对列应用numpy.log10()函数。在本例中,我们将找到列工资的对数值。计算出的数值将被存储在新的列 “logarithm_base10 “中。
代码:
# Calculate logarithm to
# base 10 on 'Salary' column
data['logarithm_base10'] = np.log10(data['Salary'])
# Show the dataframe
data
输出 :
pandas中某一列的自然对数值:
为了找到自然对数值,我们可以对列应用numpy.log()函数。在本例中,我们将找到列工资的自然对数值。计算出的数值将存储在新的列 “natural_log “中。
代码:
# Calculate natural logarithm on
# 'Salary' column
data['natural_log'] = np.log(data['Salary'])
# Show the dataframe
data
输出 :