0

For example, if I have:

A    B    
2    4   
3    5 
6    2

I would want a result of:

log 2 (4)
log 3 (5)
log 6 (2)

I've tried result = math.log(B, A), however this return an error of:

Cannot convert the series to <type 'float'>

I've had a look around at other questions but couldnt find anything related.
Is this possible in pandas, or would I have too approach this using something such as numpy?

I also had a shot at:

result = math.log(B, A).astype(float)

however this gave the same error, unfortunately

Danny
  • 435
  • 1
  • 5
  • 17

1 Answers1

2

Be smart about this. Use the properties of log.

np.log(df.B) / np.log(df.A)

0    2.000000
1    1.464974
2    0.386853
dtype: float64

This works, provided you understand that the logarithm of B with base A is the same as log B / log A in any base.

For reference, the result is the same with math.

df.apply(lambda x: math.log(x[1], x[0]), axis=1)

0    2.000000
1    1.464974
2    0.386853
dtype: float64
cs95
  • 379,657
  • 97
  • 704
  • 746