-1

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

4
  • Does this answer help? Commented Oct 7, 2022 at 9:24
  • 1
    20 * (df['xvalues'] / 1.44).apply(np.log) Commented Oct 7, 2022 at 12:55
  • 1
    always put FULL error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information in the full error/traceback. Commented Oct 7, 2022 at 12:55
  • Thnak you ,but I am getting this error "KeyError: 'xvalues'" Commented Oct 11, 2022 at 7:26

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I updated my question which shows how I read the data frame and the new error obtained after running the code df['sensi'] = df['xvalues'].apply(lambda x: 20*(np.log(x/1.44)))
Because the lambda function take the 'xvalues' column as argument, but you set that column as index
Correct xvalues is my index. Unfortunately I can't change that.May I know how to solve this
Ypu can just perform the log calc and THEN set 'xvalues' as index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.