簡體   English   中英

數據框:嘗試修復不可散列的類型:“列表”錯誤

[英]dataframe: Trying to fix unhashable type: 'list' error

我正在嘗試創建一個函數來執行列表中變量之間的多元回歸:

import statsmodels.api as sm
import pandas as pd
# Perform a multiple regression between returns of BTC,ETH,XRP and Nasdaq on all dates
df_crypto = pd.read_csv(r'F:Data/returns_on_all_dates.csv',index_col = 0)
def perform_multiple_regression(dependant_variable,independent_variables):
    X = df_crypto[[independent_variables]]
    y = df_crypto[dependant_variable]
    df_crypto.head()
    X = sm.add_constant(X)
    model = sm.OLS(y,X).fit()
    result = model.summary()
    return result
result_BTC = perform_multiple_regression('BTC_Ret',['ETH_Ret','XRP_Ret','Nasdaq_Ret'])
    # BTC return as the dependant variable    
print("The regression results summary between BTC returns on all dates and other returns is: ",result_BTC)

但這確實是Typrerror:

File "<ipython-input-1-0bacd7b983e7>", line 15, in <module>
result_BTC = perform_multiple_regression('BTC_Ret',['ETH_Ret','XRP_Ret','Nasdaq_Ret'])

File "<ipython-input-1-0bacd7b983e7>", line 7, in perform_multiple_regression
X = df_crypto[[independent_variables]]

File "D:\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2133, in __getitem__
return self._getitem_array(key)

File "D:\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2177, in _getitem_array
indexer = self.loc._convert_to_indexer(key, axis=1)

File "D:\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1256, in _convert_to_indexer
indexer = check = labels.get_indexer(objarr)

File "D:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2702, in get_indexer
indexer = self._engine.get_indexer(target._values)

File "pandas/_libs/index.pyx", line 291, in pandas._libs.index.IndexEngine.get_indexer

File "pandas/_libs/hashtable_class_helper.pxi", line 1317, in pandas._libs.hashtable.PyObjectHashTable.lookup

TypeError: unhashable type: 'list'

然后我將列表更改為元組:

result_BTC = perform_multiple_regression('BTC_Ret',('ETH_Ret','XRP_Ret','Nasdaq_Ret'))

發生另一個錯誤:

File "<ipython-input-2-33662fcca371>", line 15, in <module>
result_BTC = perform_multiple_regression('BTC_Ret',('ETH_Ret','XRP_Ret','Nasdaq_Ret'))

File "<ipython-input-2-33662fcca371>", line 7, in perform_multiple_regression
X = df_crypto[[independent_variables]]

File "D:\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2133, in __getitem__
return self._getitem_array(key)

File "D:\Anaconda3\lib\site-packages\pandas\core\frame.py", line 2177, in _getitem_array
indexer = self.loc._convert_to_indexer(key, axis=1)

File "D:\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1269, in _convert_to_indexer
.format(mask=objarr[mask]))

KeyError: "[('ETH_Ret', 'XRP_Ret', 'Nasdaq_Ret')] not in index"

我該如何解決這個問題? 提前謝謝你的幫助!

更改

X = df_crypto[[independent_variables]]

對於

X = df_crypto[independent_variables]

independent_variables已經是一個列表,因此無需放在方括號中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM