Regression by group in python pandas

前端 未结 1 1796
萌比男神i
萌比男神i 2020-12-31 23:53

I want to ask a quick question related to regression analysis in python pandas. So, assume that I have the following datasets:

 Group      Y        X
  1             


        
相关标签:
1条回答
  • 2021-01-01 00:41

    I am not sure about the type of regression you need, but this is how you do an OLS (Ordinary least squares):

    import pandas as pd
    import statsmodels.api as sm 
    
    def regress(data, yvar, xvars):
        Y = data[yvar]
        X = data[xvars]
        X['intercept'] = 1.
        result = sm.OLS(Y, X).fit()
        return result.params
    
    
    #This is what you need
    df.groupby('Group').apply(regress, 'Y', ['X'])
    

    You can define your regression function and pass parameters to it as mentioned.

    0 讨论(0)
提交回复
热议问题