0

This function:

df = pd.DataFrame(np.arange(0,1,0.01), columns = list('a'))

df = df.apply(lambda x: (math.log((x/(1-x)), 2.5)+2*math.e)/(4*math.e))  

...gives the error:

cannot convert the series to <class 'float'>

Fiddling with it and reading as much as I can, math.log seems to be returning a series. I just cannot understand why? For clarity, I am simply trying to apply the logit function to each column in the dataframe, taking whatever the value of each cell and using it as x in the function.

James
  • 81
  • 7
  • 3
    You've got it backwards: `math.log()` **takes** a float, but you're passing it a Series. Why are you using `math` in the first place when you could be using `np`? – wjandrea Feb 09 '23 at 17:29
  • 2
    I think you meant to run the apply on a specific column. `df['a'].apply(...)` – John Gordon Feb 09 '23 at 17:31
  • [Series.apply()](https://pandas.pydata.org/docs/reference/api/pandas.Series.apply.html). [DataFrame.apply()](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html). – wwii Feb 09 '23 at 17:37
  • Or maybe you meant to use [`.applymap()`](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.applymap.html) – wjandrea Feb 09 '23 at 17:38
  • I think you are all right, as I thought apply was taking the individual column values as x, but I'm at sea as to how to rewrite it. I moved to np.math.log and to specific columns but it keeps throwing me math domain error or incorrect type. – James Feb 09 '23 at 17:50
  • 1
    @James That's because `x=0` is invalid since it becomes `log(0)`. Maybe you meant to do `np.arange(0.01, 1, 0.01)`. (I was puzzling over that myself :p ) – wjandrea Feb 09 '23 at 17:53
  • 1
    That was it! It was the dummy data! You are absolutely right, thank you very much! – James Feb 09 '23 at 17:55
  • @James Oh, I just realized, `np.math is math`. What I meant was if you use `np.log` instead, it can handle arrays (here Series) instead of only scalars. Although, [using an arbitrary base requires another step](/a/25169298/4518341). – wjandrea Feb 09 '23 at 18:25

0 Answers0