Log and natural Logarithmic value of a column in pandas python

Log and natural logarithmic value of a column in pandas python is carried out using log2(), log10() and log()function of numpy. Let’s see how to

  • Get the natural logarithmic value of column in pandas (natural log  – loge())
  • Get the logarithmic value of the column in pandas with base 2  – log2()
  • Get the logarithmic value of the column in pandas with base 10 – log10()

With an example of each. First let’s create a dataframe


import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
     'Name':['George','Andrea','micheal','maggie','Ravi','Xien','Jalpa'],
   'University_Rank':[6,47,21,74,32,77,8]}

df1 = pd.DataFrame(df1,columns=['Name','University_Rank'])
print(df1)

df1 will be

Log and natural Logarithmic value of a column in pandas python 1

 

Natural logarithmic value of a column in pandas (loge) 

Natural log of the column (University_Rank) is computed using log() function and stored in a new column namely “log_value”  as shown below.


df1['log_value'] = np.log(df1['University_Rank'])
print(df1)			

natural log of a column (log to the base e) is calculated and populated, so the resultant dataframe will be

 

Log and natural Logarithmic value of a column in pandas python 2

 

Logarithmic value of a column in pandas (log2) 

log to the base 2 of the column (University_Rank) is computed using log2() function and stored in a new column namely “log2_value”  as shown below

df1['log2_value'] = np.log2(df1['University_Rank'])
print(df1)

so the resultant dataframe will be

 

Log and natural Logarithmic value of a column in pandas python 3

 

Logarithmic value of a column in pandas (log10) 

log to the base 10 of the column (University_Rank) is computed using log10() function and stored in a new column namely “log10_value”  as shown below


df1['log10_value'] = np.log10(df1['University_Rank'])
print(df1)

so the resultant dataframe will be

Log and natural Logarithmic value of a column in pandas python 4

 

Log and natural Logarithmic value of a column in pandas python next                                                                                                           Log and natural Logarithmic value of a column in pandas python next

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts