python - 如何修复: "All the input array dimensions except for the concatenation axis must match exactly" python

标签 python pandas numpy scipy correlation

我将循环结果保存在数组中,但收到错误:

all the input array dimensions except for the concatenation axis must match exactly

代码是:

r = 24
sp = np.zeros(r)
p = np.zeros(r)
for t in range(r):
    sp[t], p[t] = scipy.stats.spearmanr((df1['Pasto_1'][t:]), (df1['HumedadPasto_1'][:-t]))

我希望输出保存在 sp 中。如果您知道我做错了什么,非常感谢!

最佳答案

问题很可能出现在第一次迭代中:当 t 为 0 时,df1['Pasto_1'][t:] 是完整系列,但 >df1['HumedadPasto_1'][:-t] 为空。您可以简单地将 0 视为特殊情况并做好:

for t in range(r):
    s1 = df1['Pasto_1'][t:]
    s2 = df1['HumedadPasto_1'][:-t] if t > 0 else df1['HumedadPasto_1']
    sp[t], p[t] = scipy.stats.spearmanr(s1, s2)

关于python - 如何修复: "All the input array dimensions except for the concatenation axis must match exactly" python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58254041/

相关文章:

用于 CSV 的 Python MySQLdb 导入 MySQL 表的列数多于 CSV/列表索引超出范围?

python - MySql/Python 中的混合列求和

python - 为什么这个 Python 程序在更新字典时会引发错误?

python - 如何读取pandas dataframe中mongodb导出的Json

python - 如何在Python中的imshow图中添加轴标签?

python - 分组依据和 SUM 列

python - 在 Pandas 中使用 astype 不会给出预期的结果

python - 检查 pandas 数据框中的行是否包含特定值

python - 根据系列或数组中的索引访问 pandas 字符串列字符

python - 使用变量名称作为字典理解中的键