【问题标题】:dataframe: Trying to fix unhashable type: 'list' error数据框:尝试修复不可散列的类型:“列表”错误
【发布时间】:2023-04-03 20:44:01
【问题描述】:

我正在尝试创建一个函数来在列表中的变量之间执行多元回归:

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"

我该如何解决这个问题? 提前感谢您的帮助!

【问题讨论】:

    标签: python pandas dataframe typeerror


    【解决方案1】:

    改变

    X = df_crypto[[independent_variables]]
    

    X = df_crypto[independent_variables]
    

    independent_variables 已经是一个列表,所以不需要放双括号。

    【讨论】:

    • 有效!我认为我对 Python 真的很陌生。感谢您的帮助!
    猜你喜欢
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2018-08-07
    相关资源
    最近更新 更多