iterrows():对每一行给出相同的结果

时间:2018-12-10 10:48:04

标签: python python-3.x windows

我正在尝试为几行上的几个滞后创建自协方差。 在此示例中,我有两行和11个滞后。

在示例中,我得到了以下两行:

enter image description here

以文字显示:

 array([[164, 148, 152, 144, 155, 125, 153, 146, 138, 190, 192, 192],
   [239, 379, 105, 150, 400, 326, 134, 441, 199, 431, 203, 425]], dtype=int64)

我想获取滞后为0,1 ... 11的每一行的自协方差。

现在,我得到了以下代码:

 import statsmodels as sm
 import numpy as np
 import pandas as pd
 df = pd.read_excel("directory\\file.xlsx")

def autocov(row):
    x = sm.tsa.stattools.acovf(df.T[row], unbiased=False, demean=True, 
    fft=None, missing='none')
    autocov_df = pd.DataFrame(x)

for index,row in df.iterrows():
     print(x)

这将为我打印以下数组:

output

文本:

 [ 447.52083333  191.93229167   94.51041667  -26.36979167  -87.58333333
   -97.90104167 -121.86458333  -54.328125    -94.47916667  -31.19270833
   -12.65625      16.171875  ]
 [ 447.52083333  191.93229167   94.51041667  -26.36979167  -87.58333333
   -97.90104167 -121.86458333  -54.328125    -94.47916667  -31.19270833
  -12.65625      16.171875  ]

但是,如您所见,它为两行打印了相同的值。

我的代码在做什么错??

这是我的首选输出:

[ 447.52083333  191.93229167   94.51041667  -26.36979167  -87.58333333
  -97.90104167 -121.86458333  -54.328125    -94.47916667  -31.19270833
  -12.65625      16.171875  ]
[ 14887., -7237., 1811.,-198.5,
  2903.08333333,  -3346.41666667,   1140.33333333,  -1207.25      ,
  1141.08333333,  -3307.75      ,   1402.33333333,   -544.41666667]

1 个答案:

答案 0 :(得分:1)

您正在使用proprietarioId = id queryList = Veiculo.objects.filter(proprietario__id = proprietarioId) 进行迭代,但是您打印了恒定值:

TATE_CHOICES = (
('AC', 'Acre'), ('AL', 'Alagoas'), ('AP', 'Amapá'),
('AM', 'Amazonas'), ('BA', 'Bahia'), ('CE', 'Ceará'),
('DF', 'Distrito Federal'), ('ES', 'Espírito Santo'),
('GO', 'Goiás'), ('MA', 'Maranhão'), ('MT', 'Mato Grosso'),
('MS', 'Mato Grosso do Sul'), ('MG', 'Minas Gerais'),
('PA', 'Pará'), ('PB', 'Paraíba'), ('PR', 'Paraná'),
('PE', 'Pernambuco'), ('PI', 'Piauí'), ('RJ', 'Rio de Janeiro'),
('RN', 'Rio Grande do Norte'), ('RS', 'Rio Grande do Sul'),
('RO', 'Rondônia'), ('RR', 'Roraima'), ('SC', 'Santa Catarina'),
('SP', 'São Paulo'), ('SE', 'Sergipe'), ('TO', 'Tocantins')
)


class Pessoa(models.Model):
    nome = models.CharField(max_length=50, blank=False)
    email = models.EmailField(blank=False)
    cpf = models.CharField(max_length=11, unique=True, blank=False)
    endereco = models.CharField(max_length=50)
    numero = models.CharField(max_length=10)
    bairro = models.CharField(max_length=30)
    telefone = models.CharField(max_length=20, blank=False)
    cidade = models.CharField(max_length=20)
    estado = models.CharField(max_length=2, choices=STATE_CHOICES)

    def __str__(self):
         return str(self.nome) + ' - ' + str(self.email) 


class Veiculo(models.Model):
    marca = models.ForeignKey(Marca, on_delete=models.CASCADE, blank=False)
    modelo = models.CharField(max_length=20, blank=False)
    ano = models.CharField(max_length=7)
    placa = models.CharField(max_length=7)
    proprietario = models.ForeignKey(
        Pessoa, on_delete=models.CASCADE, blank=False, )
    cor = models.CharField(max_length=15, blank=False)


def __str__(self):
    return self.modelo + ' - ' + self.placa

@login_required def mensalista_novo(request): if request.method == 'POST': form = MensalistaForm(request.POST or None) if form.is_valid(): form.save() return redirect('core_lista_mensalista') else: form = MensalistaForm return render(request, 'core/lista_mensalistas.html', {'form': form}) 不依赖于index, rowfor index,row in df.iterrows(): print(autocov_df.T)

您需要使用迭代变量来查看差异,例如:

autocov_df.T

在您的示例中,您没有调用index并且该函数中没有return语句:

row

请注意,您的参数命名可能会引起误解。