I have created a pandas dataframe (called df) as follows:
import pandas as pd
import numpy as np
ds = {'Trend' : [1,1,1,1,1,
2,2,2,2,2,
3,3,3,3,3,
4,4,4,4,4,
5,5,5,5,5],
'Cycle' : [
14,88,50,119,107,
32,111,41,88,38,
37,72,79,98,69,
62,38,86,26,30,
52,91,122,90,127
]
}
df = pd.DataFrame(data=ds)
The dataframe looks like this:
print(df)
Trend Cycle
0 1 14
1 1 88
2 1 50
3 1 119
4 1 107
5 2 32
6 2 111
7 2 41
8 2 88
9 2 38
10 3 37
11 3 72
12 3 79
13 3 98
14 3 69
15 4 62
16 4 38
17 4 86
18 4 26
19 4 30
20 5 52
21 5 91
22 5 122
23 5 90
24 5 127
I need to create a new column (called ArrayToBeMade) which contains:
- a list of three elements
- each element is the last value of the column
Trend(which we get by iterating through each record)
The resulting dataframe would look like this:
Can someone help me, please?

Use
GroupBy.lastwith mapping columns withSeries.shift, last convert 3 columns DataFrame to numpy array and then to lists: