python - 数据框:尝试修复不可散列的类型:“列表”错误
问题描述
我正在尝试创建一个函数来在列表中的变量之间执行多元回归:
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
已经是一个列表,所以不需要放双括号。
推荐阅读
- arrays - 查找特定的数组索引
- android - 手势不适用于 Android 上的 ListView 项目
- cygwin - cygwin mpicxx not found
- c# - 从曲线c#中提取点坐标(x,y)
- bash - 如何将文件移动到文件夹中并将它们从文件夹中取出
- video.js - videoJs inactivityTimeout 选项不起作用
- android - RxJava - 使用第二个 observable 中第一个 observable 的结果顺序运行两个 observable
- php - 请求 ajax 不适用于 php 验证
- jsf - OmniFaces o:validateOneOrMore 跳过有条件禁用的字段
- apache-spark - Spark SaveAsTable 需要很多时间