How to formulate new DataFrame's form a given dataFrame for feature selection in Python

Viewed 37

My Initial DataFrame Looks like given below(just for example, though it contains different values with more number of inputs)

close
1 
2 
3 
4
5
6

Now I need to select hyper parameter and based on that I need to create new DataFrames every time. Suppose my N starts with N=1 then the new dataFrame should look like

x1 Y
1  2
2  3
3  4
4  5

and it will continue till the length of the original dataFrame. Once it done it will move to the next hyperparameter with N=2 and then my new dataFrame should look like

X1 X2 Y
1  2  3
2  3  4
3  4  5
4  5  6

and soon, it will continue till certain hyper parameter N.

1 Answers

I hope I've understood you correctly. This example will create new dataframe from df['close'] based on the N:

N = 1

df_new = pd.DataFrame(
    {
        **{f"X{i+1}": df["close"].shift(-i)[: len(df) - N] for i in range(N)},
        "Y": df["close"].shift(-N)[: len(df) - N],
    }
).astype(int)
print(df_new)

Prints:

   X1  Y
0   1  2
1   2  3
2   3  4
3   4  5
4   5  6

for N = 2:

   X1  X2  Y
0   1   2  3
1   2   3  4
2   3   4  5
3   4   5  6
Related