Skip to content
Advertisement

Simple calculation on table. Please help me to make my code more effective

Please help me to make my code more effective. This is my df:

df = pd.DataFrame([['A', 80], ['A', 64], ['A', 55], ['B', 56], ['B', 89], ['B', 73], ['C', 78], ['C', 100], ['C', 150], ['C', 76], ['C', 87]], columns=['Well', 'GR'])
Well    GR
A       80
A       64
A       55
B       56
B       89
B       73
C       78
C       100
C       150
C       76
C       87


Please help me to find the Vshale. Vshale on each well = GR – GR(min) / GR(max) – GR(min). This is my desired result:

Well    GR      Vshale
A       80      1
A       64      0.36
A       55      0
B       56      0
B       89      1
B       73      0.515151515
C       78      0.027027027
C       100     0.324324324
C       150     1
C       76      0
C       87      0.148648649


This code is work for me, but, I should create a new column that consists of GRMax and GRMin and merge it into my previous df. I am looking for a more effective way without adding GRmin and GRmax on my original df. Thank you.

df1 = df.groupby(['Well']).agg({'GR': ['min', 'max']}).reset_index()
df1.columns = list(map(''.join, df1.columns.values))
df2 = pd.merge (df, df1, on = 'Well', how = 'left')
df2['Vshale'] = (df2['GR'] - df2['GRmin'])/(df2['GRmax'] - df2['GRmin'])

Advertisement

Answer

one string solution with method transform:

df['Vshale'] = df.groupby('Well').transform(lambda x: (x - np.min(x))/(np.max(x) - np.min(x)))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement