亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

iterrows() :對每一行給出相同的結果

iterrows() :對每一行給出相同的結果

慕標琳琳 2021-09-11 20:50:31
我正在嘗試為幾行的幾個滯后創建自協方差。在這個例子中,我得到了兩行和 11 個滯后。在我的示例中,我得到了以下兩行:在文本中: 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 的每一行的自協方差?,F在,我得到了以下代碼: 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)這會向我打印以下數組:在文本中: [ 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 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

您正在迭代index, row但打印常量值:


for index,row in df.iterrows():

    print(autocov_df.T)

autocov_df.T不依賴于index或row。


您需要使用迭代變量來查看差異,例如:


for index,row in df.iterrows():

    y = some_function(index,row)

    print(y)

在您的示例中,您不調用autocov并且該函數中沒有 return 語句:


def autocov(row):

    x = sm.tsa.stattools.acovf(df.T[row], unbiased=False, demean=True, 

    fft=None, missing='none')

    autocov_df = pd.DataFrame(x)

    return(autocov_df)


for index,row in df.iterrows():

     x = autocov(index)

     print(x)

請注意,您的參數命名可能會產生誤導。


查看完整回答
反對 回復 2021-09-11
  • 1 回答
  • 0 關注
  • 258 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號