Please help me to make my code more effective. This is my df:
JavaScript
x
2
1
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'])
2
JavaScript
1
15
15
1
Well GR
2
A 80
3
A 64
4
A 55
5
B 56
6
B 89
7
B 73
8
C 78
9
C 100
10
C 150
11
C 76
12
C 87
13
14
15
Please help me to find the Vshale. Vshale on each well = GR – GR(min) / GR(max) – GR(min). This is my desired result:
JavaScript
1
15
15
1
Well GR Vshale
2
A 80 1
3
A 64 0.36
4
A 55 0
5
B 56 0
6
B 89 1
7
B 73 0.515151515
8
C 78 0.027027027
9
C 100 0.324324324
10
C 150 1
11
C 76 0
12
C 87 0.148648649
13
14
15
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.
JavaScript
1
6
1
df1 = df.groupby(['Well']).agg({'GR': ['min', 'max']}).reset_index()
2
df1.columns = list(map(''.join, df1.columns.values))
3
df2 = pd.merge (df, df1, on = 'Well', how = 'left')
4
df2['Vshale'] = (df2['GR'] - df2['GRmin'])/(df2['GRmax'] - df2['GRmin'])
5
6
Advertisement
Answer
one string solution with method transform:
JavaScript
1
2
1
df['Vshale'] = df.groupby('Well').transform(lambda x: (x - np.min(x))/(np.max(x) - np.min(x)))
2