0

I have a dataframe with multiple columns but I want to focus on a specific one. From this column called "Value" I would like to create N columns where N is a window of the column "Value". I'll try to explain it better in the following image

screenshot of Excel showing the windowing

In this particular case, my parameter N is equal to 5, but of course it can change dynamically. Starting from the first row, I want to take the next 5 values and transpose them to columns, the same for the second row, and so on so forth..

Can anyone help me to solve this problem?

My idea was to try to iterate over rows and assign values to subset of columns like this

df[['D1','D2','D3','D4','D5']].loc[1] = ['1','2','3','4','5']

but it seems that it's not possible.

1 Answer 1

1

You are looking for shift:

for i in range(1, 6):
    df[f"D{i}"] = df["VALUE"].shift(-i)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.