how to subtract two values in my dataframe

Question:

I get this csv from my program and i want to get the "thickness"/difference of each of my Depth values.
So for example 3998 - 4002 = -4 is there a way to use this csv or is it useless because i couldn’t find anything that would solve my problem.

df = pd.read_csv ('/PermeabilityData.csv',sep=";") 
display(df)

a = df['Depth [ft]'].loc[data.index[0]]
    Depth [ft]  Permeability [mD]
0   3998-4002   180
1   4002-4004   150
2   4004-4006   200
3   4006-4008   140
4   4008-4010   160
Asked By: Dani

||

Answers:

A possible solution:

df['Depth [ft]'].map(eval)

Output:

0   -4
1   -2
2   -2
3   -2
4   -2
Name: Depth [ft], dtype: int64
Answered By: PaulS

This could also be done:

pd.eval(df.iloc[:,0])

[-4, -2, -2, -2, -2]
Answered By: Anoushiravan R

In lieu of using the eval, here is one way to do it

df['Depth[ft]'].str.split('-',expand=True).astype(int).apply(lambda x: x[0]-x[1], axis=1)
0   -4
1   -2
2   -2
3   -2
4   -2
dtype: int32
Answered By: Naveed
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.