Confused
Confused

Reputation: 353

Logarithmic operation on pandas dataframe-Key value error

I have a data frame as shown below.it's name is 'df_thd_funct' .I need to apply logarithmic equation on xvalue columns and need to create a new column.Below is my code.

df_thd_funct['sensi'] = 20*(np.log(df_thd_funct.xvalues/1.44)) 

xvalues SFM_376_1.62_-40    
0.0189     0.0190                       
0.0200     0.0187               
0.0225     0.0167              
0.0239     0.0191

The error I am getting is given below. What am I missing?

enter image description here

My complete code is given below.

df_thd_funct = pd.read_csv("A2_THD_Ratio_Stepped_Sweep_Corners_PVT_lab01.txt",delim_whitespace=True,index_col=False)
df_thd_funct.set_index('xvalues', inplace=True)
check_for_nan = df_thd_funct.isnull().values.any()
df_thd_funct = df_thd_funct.T
print (check_for_nan)
df_thd_funct.head()

After running df_thd_funct['sensi'] = df_thd_funct['xvalues'].apply(lambda x: 20*(np.log(x/1.44)))

The new error obtained is given below.

enter image description here

Upvotes: -1

Views: 44

Answers (1)

imburningbabe
imburningbabe

Reputation: 792

You can't give np.log a pd.Series as argument, you have to use lambda function:

import pandas as pd

df = pd.DataFrame({'xvalues':[0.0189,0.0200,0.0225,0.0239]})

df['sensi'] = df['xvalues'].apply(lambda x: 20*(np.log(x/1.44))) 

Output:

   xvalues      sensi
0   0.0189 -86.664729
1   0.0200 -85.533322
2   0.0225 -83.177662
3   0.0239 -81.970399

Upvotes: 1

Related Questions