Проблема с индексацией в операторах if-elif (индексатор несовместим с сериями) - PullRequest
0 голосов
/ 31 января 2020

Я пытаюсь запустить следующий код, но у меня проблемы с функцией .lo c. Я пытаюсь (1) отсортировать каждую строку по типу строки, (2) использовать данные из 4 столбцов в этой строке как 4 различных индекса для другого кадра данных, а затем (3) взять произведение этих 4 новых проиндексированных элемента и создайте новый столбец в кадре данных (TL4_tranpose) с этими данными.

# run through list of each child
for i in range(56):
    # if, elif and else separating them into groups of theorists [Size, Material, Mass or Random]
    # same method as above for fitting the pretest

    # grab each subjects responses
    sub_choices = TL4_transpose[['learn_1', 'learn_2', 'learn_3', 'learn_4', 'Theory', 'ParticipantID']].iloc[i]

    if TL4_transpose.Theory[i] == 'Size':
        Size_Learn = pd.DataFrame()
        prob_Size = []

        for j in range(4):

            # look up the prob of that participant's choices happening at each of 8 trials
            Size_Learn = Size_Learn.append([sizeModelLearn.iloc[j][sub_choices.iloc[j]]])

        # take the product of participant's fit to the model; assuming independence due to no feedback
        prob_Size = abs(Size_Learn.product())

        TL4_transpose.loc[str(i), 'LearnProb'] = prob_Size  **<-- where the error pops up **
        TL4_transpose.loc[str(i), 'LearnLog'] = math.log(prob_Size)

    elif TL4_transpose.Theory[i] == 'Material':
        mat_prob = 2
        TL4_transpose.loc[str(i), 'LearnProb'] = 'Material'
        TL4_transpose.loc[str(i), 'LearnLog'] = mat_prob

    elif TL4_transpose.Theory[i] == 'Mass':
        mass_prob = 3
        TL4_transpose.loc[str(i), 'LearnProb'] = 'Mass'
        TL4_transpose.loc[str(i), 'LearnLog'] = mass_prob

    elif TL4_transpose.Theory[i] == 'Random': 
        ran_prob = 0.2 ** 4
        TL4_transpose.loc[str(i), 'LearnProb'] = 'Random'
        TL4_transpose.loc[str(i), 'LearnLog'] = ran_prob

    # take the log for simplification
    #TL4_transpose.loc[i, 'log(test)'] = math.log(prob_Choice)

TL4_transpose

, но он продолжает давать мне код ошибки:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-517-e664a51dffaf> in <module>()
     19         prob_Size = abs(Size_Learn.product())
     20 
---> 21         TL4_transpose.loc[str(i), 'LearnProb'] = prob_Size
     22         TL4_transpose.loc[str(i), 'LearnLog'] = math.log(prob_Size)
     23 

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in __setitem__(self, key, value)
    187             key = com._apply_if_callable(key, self.obj)
    188         indexer = self._get_setitem_indexer(key)
--> 189         self._setitem_with_indexer(indexer, value)
    190 
    191     def _validate_key(self, key, axis):

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _setitem_with_indexer(self, indexer, value)
    467 
    468             if isinstance(value, ABCSeries):
--> 469                 value = self._align_series(indexer, value)
    470 
    471             info_idx = indexer[info_axis]

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _align_series(self, indexer, ser, multiindex_indexer)
    775             return ser.reindex(ax)._values
    776 
--> 777         raise ValueError('Incompatible indexer with Series')
    778 
    779     def _align_frame(self, indexer, df):

ValueError: Incompatible indexer with Series

Есть ли способ чтобы исправить это?

Редактировать:

Ранее в своей записной книжке я смог запустить эту ячейку:

Pretest_Probs = pd.DataFrame()

for j in range(93):

    # grab participant j's data
    sub_choices = PrePost[['pre1', 'pre2', 'pre3', 'pre4', 'pre5','pre6', 'pre7', 'pre8']].iloc[j]

    # need blank dataframes each time you run the loop, else it'll output a single columns with (i * j) values
    sub_Size_Pre = pd.DataFrame()
    prob_sub_Size = []

    for i in range(8):

        # look up the prob of that participant's choices happening at each of 8 trials
        sub_Size_Pre = sub_Size_Pre.append([sizeModelPre.iloc[i][sub_choices.iloc[i]]])

    # take the product of participant's fit to the model; assuming independence due to no feedback
    prob_sub_Size = abs(sub_Size_Pre.product())

    # take the log for simplification
    size_log_like = math.log(prob_sub_Size)

    Pretest_Probs.loc[str(j), 0] = size_log_like

Pretest_Probs.head()

, которая выполняет тот же процесс, что и код с ошибкой, но не включает операторы if / elif.

Когда я меняю

TL4_transpose.loc[str(i), 'LearnProb'] = prob_Size

на

TL4_transpose.loc[str(i)]['LearnProb'] = prob_Size

Я получаю

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis)
   1789                 if not ax.contains(key):
-> 1790                     error()
   1791             except TypeError as e:

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in error()
   1784                                .format(key=key,
-> 1785                                        axis=self.obj._get_axis_name(axis)))
   1786 

KeyError: 'the label [15] is not in the [index]'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-528-d95879b9ab92> in <module>()
     21         prob_Size = abs(Size_Learn.product())
     22 
---> 23         TL4_transpose.loc[str(i)]['LearnProb'] = prob_Size
     24         TL4_transpose.loc[str(i), 'LearnLog'] = math.log(prob_Size)
     25 

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1476 
   1477             maybe_callable = com._apply_if_callable(key, self.obj)
-> 1478             return self._getitem_axis(maybe_callable, axis=axis)
   1479 
   1480     def _is_scalar_access(self, key):

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1909 
   1910         # fall thru to straight lookup
-> 1911         self._validate_key(key, axis)
   1912         return self._get_label(key, axis=axis)
   1913 

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_key(self, key, axis)
   1796                 raise
   1797             except:
-> 1798                 error()
   1799 
   1800     def _is_scalar_access(self, key):

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in error()
   1783                 raise KeyError(u"the label [{key}] is not in the [{axis}]"
   1784                                .format(key=key,
-> 1785                                        axis=self.obj._get_axis_name(axis)))
   1786 
   1787             try:

KeyError: 'the label [15] is not in the [index]'

1 Ответ

0 голосов
/ 31 января 2020

Понятия не имею почему, но удаление строки, которая вычисляет вероятность

например,

TL4_transpose.loc[str(i), 'LearnProb'] = prob_Size

и запуск только строки прямо под ней, которая принимает натуральный логарифм, заставила код работать ...

# run through list of each child
for i in range(56):
    # if, elif and else separating them into groups of theorists [Size, Material, Mass or Random]
    # same method as above for fitting the pretest

    # grab each subjects responses
    sub_choices = TL4_transpose[['learn_1', 'learn_2', 'learn_3', 'learn_4', 'Theory', 'ParticipantID']].iloc[i]

    if TL4_transpose.Theory[i] == 'Size':
        Size_Learn = pd.DataFrame()
        prob_Size = []

        for j in range(4):

            # look up the prob of that participant's choices happening at each of 4 trials
            Size_Learn = Size_Learn.append([sizeModelLearn.iloc[j][sub_choices.iloc[j]]])

        # take the product of participant's fit to the model; assuming independence due to no feedback
        prob_Size = abs(Size_Learn.product())

        #TL4_transpose.loc[i]['LearnProb'] = prob_Size
        TL4_transpose.loc[i, 'LearnLog'] = math.log(prob_Size)

    elif TL4_transpose.Theory[i] == 'Material':
        Mat_Learn = pd.DataFrame()
        prob_Mat = []

        for j in range(4):

            # look up the prob of that participant's choices happening at each of 4 trials
            Mat_Learn = Mat_Learn.append([matModelLearn.iloc[j][sub_choices.iloc[j]]])

        # take the product of participant's fit to the model; assuming independence due to no feedback
        prob_Mat = abs(Mat_Learn.product())

        #TL4_transpose.loc[str(i), 'LearnProb'] = 'Material'
        TL4_transpose.loc[i, 'LearnLog'] = math.log(prob_Mat)

    elif TL4_transpose.Theory[i] == 'Mass':
        Mass_Learn = pd.DataFrame()
        prob_Mass = []

        for j in range(4):

            # look up the prob of that participant's choices happening at each of 4 trials
            Mass_Learn = Mass_Learn.append([massModelLearn.iloc[j][sub_choices.iloc[j]]])

        # take the product of participant's fit to the model; assuming independence due to no feedback
        prob_Mass = abs(Mass_Learn.product())

        #TL4_transpose.loc[str(i), 'LearnProb'] = 'Mass'
        TL4_transpose.loc[i, 'LearnLog'] = math.log(prob_Mass)

    elif TL4_transpose.Theory[i] == 'Random': 
        prob_Ran = 0.2 ** 4
        #TL4_transpose.loc[str(i), 'LearnProb'] = 'Random'
        TL4_transpose.loc[i, 'LearnLog'] = math.log(prob_Ran)

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