Part 1:
I choose not to use iteration, assign new values to the whole column instead.
import pandas as pd
import numpy as np
df = pd.DataFrame({'Name': ['AS', 'AS', 'AS', 'DB', 'DB', 'DB'],
'Depth': [15, 16, 17, 10, 11, 12],
'Value':[100, 200, 300, 200, 300, 400]
})
Output:
Name Depth Value
0 AS 15 100
1 AS 16 200
2 AS 17 300
3 DB 10 200
4 DB 11 300
5 DB 12 400
use len to get the length of the column.
df[column][0] to get the initial value. If you do have a specific initial value then just skip this step. Assign your initial value to it.
ini_1 = df['Depth'][0] # initial value
ini_2 = df['Value'][0] # initial value
length = len(df)
step_1 = 0.1
step_2 = 10
df['Depth'] = np.arange(ini_1, ini_1+length*step_1, step_1)
df['Value'] = np.arange(ini_2, ini_2+length*step_2, step_2)
output
Name Depth Value
0 AS 15.0 100
1 AS 15.1 110
2 AS 15.2 120
3 DB 15.3 130
4 DB 15.4 140
5 DB 15.5 150
Since we don't know the variant regulation between Name and Depth, but it's another aspect that avoids iterate into every single row.
Part 2:
Suppose every name-depth group expands to 10 items
and follow the increment of 0.1 and 10 on Depth and Value respectively.
Here's the step:
- load the dataframe
import pandas as pd
import numpy as np
df = pd.DataFrame({'Name': ['AS', 'AS', 'AS', 'DB', 'DB', 'DB'],
'Depth': [15, 16, 17, 10, 11, 12],
'Value':[100, 200, 300, 200, 300, 400]
})
- expand
df to 10X:
dfn = pd.concat([df]*10,ignore_index=False).sort_index()
- It's an arithmetic progression:
for Depth a = 0, d = 0.1, length = 10
for Value a = 0, d = 10, length = 10
Taking them as vector(1D array) summation in each Name-Depth group:
a = 0
d_depth = 0.1
d_value = 10
length = 10
arithmetric_1 = [round(a + d_depth * (n - 1),2) for n in range(1, length + 1)] # arithmetic progression series for Depth
arithmetric_2 = [round(a + d_value * (n - 1),2) for n in range(1, length + 1)] # arithmetic progression series for Value
- the main part
for i in set(dfn.index):
dfn.loc[i,'Depth'] = dfn.loc[i,'Depth'].array + arithmetric_1
dfn.loc[i,'Value'] = dfn.loc[i,'Value'].array + arithmetric_2
summary:
now you get dataframe dfn as the result base on the assumption. This manipulation try to decrease the loop-times, and use vector aspect to deal with the problem(if you have huge datasets).
Name Depth Value
0 AS 15.0 100
0 AS 15.1 110
0 AS 15.2 120
0 AS 15.3 130
0 AS 15.4 140
0 AS 15.5 150
0 AS 15.6 160
0 AS 15.7 170
0 AS 15.8 180
0 AS 15.9 190
1 AS 16.0 200
1 AS 16.1 210
1 AS 16.2 220
1 AS 16.3 230
1 AS 16.4 240
1 AS 16.5 250
1 AS 16.6 260
1 AS 16.7 270
1 AS 16.8 280
1 AS 16.9 290
2 AS 17.0 300
2 AS 17.1 310
:
: