датафрейм: Попытка исправить непредсказуемый тип: ошибка списка - PullRequest
0 голосов
/ 30 апреля 2018

Я пытаюсь создать функцию для выполнения множественной регрессии между переменными в списке:

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 голосов
/ 30 апреля 2018

Изменение

X = df_crypto[[independent_variables]]

для

X = df_crypto[independent_variables]

independent_variables - это уже список, поэтому не нужно ставить двойные скобки.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...