简体   繁体   中英

how to get the slope of multiple columns in Python data frame

I have the below data frame that carries 4 columns of scores. how do I find the slope of these 4 scores for each individual ID in my data frame?

ID  t1  t2  t3  t4
a   1   2   3   4
b   3   2   1   
c   4   2   1   2
d   2   3   4   5
e   0   2   3   4

I would like the slope be appended back the same data frame and show the following after the slope is calculated.

ID  Slope
a   1
b   -1
c   -0.7
d   1
e   1.3

you can use sklearn (or probably scipy ) for this. Example:

import sklearn

model = sklearn.linear_model.LinearRegression()
def get_coeff(row, model=model):
    # fit a row assuming points are separated by unit length and return the slope.
    
    row = row.copy().dropna()
    model.fit(np.arange(len(row)).reshape(-1,1), row.values.reshape(-1,1))
    slope = model.coef_[0][0]
    return slope


df["slope"] = df.apply(get_coeff, axis=1)

output:

    t1  t2  t3   t4  slope
ID
a    1   2   3  4.0    1.0
b    3   2   1  NaN   -1.0
c    4   2   1  2.0   -0.7
d    2   3   4  5.0    1.0
e    0   2   3  4.0    1.3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM