【发布时间】:2020-05-17 09:18:20
【问题描述】:
我正在尝试运行以下代码,但我在使用 .loc 函数时遇到了问题。我要做的是(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]'
【问题讨论】:
-
你能创建一个minimal reproducible example吗?
-
我使用类似的代码进行了编辑,当我更改 .loc 的输入时会发生什么。不知道还有什么方法可以解决这个问题......
-
我希望您能将问题归结为一个可能包含一两个 if/then 语句的小数据集。以目前的形式,我必须解析比我想要的更多的代码。也就是说,我不会打扰。我想让您知道这一点,以便您有机会更改它并提高获得有用答案的几率。祝你好运!
-
当然,感谢您的帮助!感谢您的评论,我以后会注意的。
标签: python pandas dataframe if-statement indexing