Given a parameter p
, be any float or integer.
For example, let p=4
time | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
Numbers | a1 | a1*(0.5)^(1/p)^(2-1) | a1*(0.5)^(1/p)^(2-1) | a1*(0.5)^(1/p)^(3-1) | a1*(0.5)^(1/p)^(4-1) |
Numbers | nan | a2 | a2*(0.5)^(1/p)^(3-2) | a2*(0.5)^(1/p)^(4-2) | a2*(0.5)^(1/p)^(5-2) |
Numbers | nan | nan | a3 | a3*(0.5)^(1/p)^(4-3) | a3*(0.5)^(1/p)^(5-3) |
Numbers | nan | nan | nan | a4 | a4*(0.5)^(1/p)^(5-4) |
Number | nan | nan | nan | nan | a5 |
Final Results | a1 | sum of column 2 | sum of column 3 | sum of column 4 | sum of column 5 |
Numbers like a1,a2,a3,a4,a5,…,at is given, our goal is to find the Final Results
. Combining the answer provided by mozway
, I wrote the following function which works well. It is a matrix
way to solve the problem.
JavaScript
x
7
1
def hl(p,column):
2
a = np.arange(len(copy_raw))
3
factors = (a[:,None]-a)
4
factors = np.where(factors<0, np.nan, factors)
5
inter = ((1/2)**(1/p))**factors
6
copy_raw[column] = np.nansum(copy_raw[column].to_numpy()*inter, axis=1)
7
However, I don’t think this method will work well if we are dealing with large dataframe. Are there any better way to fix the problem? (In this case, faster = better.)
Advertisement
Answer
Assuming your number of rows is not too large, you can achieve this with numpy broadcasting:
First create a 2D array of factors:
JavaScript
1
9
1
a = np.arange(len(df))
2
factors = (a[:,None]-a)
3
factors = np.where(factors<0, np.nan, factors)
4
# array([[ 0., nan, nan, nan, nan],
5
# [ 1., 0., nan, nan, nan],
6
# [ 2., 1., 0., nan, nan],
7
# [ 3., 2., 1., 0., nan],
8
# [ 4., 3., 2., 1., 0.]])
9
Then map to your data and sum:
JavaScript
1
2
1
df['number2'] = np.nansum(df['number'].to_numpy()*(1/2)**factors, axis=1)
2
example output:
JavaScript
1
7
1
Index Time number number2
2
0 0 1997-WK01 1 1.0000
3
1 1 1997-WK02 2 2.5000
4
2 2 1997-WK03 3 4.2500
5
3 3 1997-WK04 2 4.1250
6
4 4 1997-WK05 4 6.0625
7
intermediate:
JavaScript
1
8
1
df['number'].to_numpy()*(1/2)**factors
2
3
# array([[1. , nan, nan, nan, nan],
4
# [0.5 , 2. , nan, nan, nan],
5
# [0.25 , 1. , 3. , nan, nan],
6
# [0.125 , 0.5 , 1.5 , 2. , nan],
7
# [0.0625, 0.25 , 0.75 , 1. , 4. ]])
8