Regression column in pandas

211 Views Asked by At

Let's say I have a pandas dataframe df with some trivial indexing, e.g. 0,1,2,... and just one column 'Values' which contains numerical data. I would like to add a new column 'Trend' such that df['Trend'][n] is a coefficient of linear regression based on 5 consecutive values from the original values column: 'df['Values'][n-5] ... 'df['Values'][n-1]. The first 5 values are of course undefined, so let's say we put them to be NaN. Is there an easy way to do that?

1

There are 1 best solutions below

0
On

Have you tried checking the index and doing different things, depending on where you are in the DataFrame?

for index, row in df.iterrows():
    if index<5:
        df['Trend'][index] = 'NaN'
    else:
        df['Trend'][index] = df['Values'][index-5]...