AtlanticWorker
AtlanticWorker

Reputation: 79

How do I use the mathematical Log function a Pandas DataFrame

I have a Pandas DataFrame and I am calculating new columns based on various mathematical formulae applied to the existing columns. I cannot make the math.log function work. I get the following error:

File "/Users/XXXXX/Documents-Offline/Python/maths_play.py", line 27, in <module>
    df['Isbt']=(math.log10(df['qc/pa']))   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/core/series.py", line 118, in wrapper
    "{0}".format(str(converter))) TypeError: cannot convert the series to <class 'float'>

This is what I have so far. Everything is working up to the line with the Log function.

import pandas as pd
import math

data= pd.read_csv (r'/Users/XXXXXX/Desktop/snippit.csv')

df=pd.DataFrame(data, columns=['Depth','Cone','Friction','Pore'])

DenSea=10.26 #Average Sea water density
unitW=19.0 #Soils Unit weight
alpha=0.5
atmos=0.1

df['Ambient']=df['Depth']* DenSea
df['qt']=df['Cone']+(df['Pore']*(1-alpha))
df['Rf']=(df['Friction']/df['qt'])*100
df['qc/pa']=df['Cone']*atmos
df['Sigmavo']=df['Depth']*unitW
df['SigmaEF']=df['Sigmavo']-df['Ambient']
df['Qt1']=((df['qt']*1000)-df['Sigmavo'])/df['SigmaEF']
df['Fr']=((df['Friction']*1000)/((df['qt']*1000)-df['Sigmavo']))*100
df['Bq']=((df['Pore']*1000)-df['Ambient'])/((df['qt']*1000)-df['Sigmavo'])
df['Isbt']=(((3.47-(math.log(df['qc/pa'])))**2)+(((math.log(df['Rf']))+1.22)**2))**0.5


#data['Depth']*=-1 #Leave to last - invert depths - below seabed

print(df)

A snippet of the imported CSV.

    Depth    Cone  Friction    Pore
0   0.012  0.0009    0.0001  0.0002
1   0.025  0.0001    0.0001  0.0002
2   0.036  0.0014    0.0001  0.0008
3   0.050  0.0097    0.0006  0.0004
4   0.062  0.0146    0.0007  0.0004
5   0.074  0.0144    0.0006  0.0009
6   0.086  0.0116    0.0013  0.0013
7   0.098  0.0168    0.0017  0.0017
8   0.110  0.0268    0.0014  0.0029
9   0.123  0.0329    0.0019  0.0037
10  0.135  0.0459    0.0029  0.0043

Upvotes: 0

Views: 281

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

math.log only takes scalar (int, float, etc). Use numpy's log instead:

df['Isbt'] = (((3.47-(np.log(df['qc/pa'])))**2)+(((np.log(df['Rf']))+1.22)**2))**0.5(np.log10(df['qc/pa']))

Upvotes: 1

Related Questions