【发布时间】:2021-03-04 20:55:18
【问题描述】:
我有以下熊猫数据框:
>>>> wavelength veg asph veg1 asph1
band
B1 449.9 0.037 0.055 0.63 0.75
B2 496.6 0.044 0.063 0.48 0.76
B3 560.0 0.08 0.073 0.45 0.66
...
我想为每个波段索引创建 x 和 y 变量,例如
Xb1=[0.63 ,0.75]
Yb1=[ 0.037 ,0.055]
Xb2=[0.48 ,0.76]
Yb2=[ 0.044 ,0.063]
*“b1”和“b2”只是为了更容易理解数字来自哪一行。
我尝试只选择一行,然后使用 loc 和 iloc 选择特定数字,如下所示:
bnd=['B1','B2','B3','B4','B5','B6','B7','B8','B9','B10','B11','B12']
#df=df.set_index('bands')
for b in bnd:
print(b)
x=df.loc[b].iloc[:,3:].astype(str).astype(float)
y=df.loc[b].iloc[:,1:3].astype(str).astype(float)
但我收到此错误消息:
IndexingError:索引器过多
我还不能确定正确的书写方式,所以我得到了这些数字。
我的最终目标是能够获得 X 和 y 的正确数字,以便计算两者之间的线性回归(换句话说:我想确定 X 和 y为每个指标计算线性回归)。
编辑:
x 和 y 应该插入到这部分来计算线性回归:
# Fit with polyfit
b, m = polyfit(x, y, 1)
plt.plot(x, y, '.')
plt.title('B1')
plt.plot(x, b + m * x, '-')
plt.show()
print('m:',m , 'b:',b)
所以它们应该是数字
【问题讨论】:
-
@BillHuang 我已经编辑过了
-
@BilHuang b1 和 b2 与行相关,我把它放在那里是为了便于理解数字来自哪一行
-
df.to_dict('index') 呢?索引将是键,值将是另一个字典,以列名作为键
-
你需要列表中的这些变量吗?
-
@Qamar Abbas 是的