【发布时间】:2023-03-13 08:10:02
【问题描述】:
我有一个这样的 df:
Allotment Year NDVI A_Annex Bachelor
A_Annex 1984 1.0 0.40 0.60
A_Annex 1984 1.5 0.56 0.89
A_Annex 1984 2.0 0.78 0.76
A_Annex 1985 3.4 0.89 0.54
A_Annex 1985 1.6 0.98 0.66
A_Annex 1986 2.5 1.10 0.44
A_Annex 1986 1.7 0.87 0.65
Bachelor 1984 8.9 0.40 0.60
Bachelor 1984 6.5 0.56 0.89
Bachelor 1984 4.2 0.78 0.76
Bachelor 1985 2.4 0.89 0.54
Bachelor 1985 1.7 0.98 0.66
Bachelor 1986 8.9 1.10 0.44
Bachelor 1986 9.6 0.87 0.65
我想运行基于 groupby 的回归。我想将每个唯一的 Allotment 及其 NDVI 值及其关联列进行回归。所以我想用Allotment A_Annex 及其关联的NDVI 对列A_Annex 进行回归。然后我想用Bachelor 做同样的事情。本质上,我想将列与关联的Allotment 匹配,然后将列中的值与相应的NDVI 值进行回归。
我可以为这样的一个分配做到这一点:
stat=merge.groupby(['Allotment']).apply(lambda x: sp.stats.linregress(x['A_Annex'], x['NDVI']))
但我需要继续更改 sp.stats.linregress(x['A_Annex'], x['NDVI'])) 中的 x 值,我想避免这种情况。
【问题讨论】:
-
对不起,但我不明白为什么你在你的 sn-p 之前加上“我可以为这样的一个分配执行此操作:” - 它会为每个
Allotment值执行此操作。我完全不明白你的问题。 -
不确定这是否绝对是最好的方法,但我可以看到这里的简单
for循环没有真正的缺点:for x in df.Allotment.unique(): your_regression_code -
顺便说一句,我认为现在进行 pandas 回归最推荐的方法是使用 statsmodels(我在上面添加了标签)
标签: python pandas scipy statsmodels