I'm trying to run the following code, but I am having trouble with the .loc function. What I am trying to do is (1) sort each row by the type of row they are, (2) use the data from 4 columns within that row as 4 different indices for another dataframe, then (3) take the product of those 4 newly indexed items and create a new column in a dataframe (TL4_tranpose) with that data.
# 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
but it keeps giving me the error code:
---------------------------------------------------------------------------
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
Is there a way to fix this?
Edit:
Earlier on in my notebook, I was able to run this cell:
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()
which does a similar process as the code with the error, but does not include the if/elif statements.
When I change
TL4_transpose.loc[str(i), 'LearnProb'] = prob_Size
to
TL4_transpose.loc[str(i)]['LearnProb'] = prob_Size
I get
---------------------------------------------------------------------------
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]'
Aucun commentaire:
Enregistrer un commentaire