dataframe:尝试修复不可用的类型:' list'错误

时间:2018-04-30 02:02:56

标签: python pandas dataframe typeerror

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

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"

如何解决此问题? 感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

更改

X = df_crypto[[independent_variables]]

X = df_crypto[independent_variables]

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