Python 为什么这个值显得模棱两可?
我完全搞不懂为什么我在这段代码上会出现ValueError;谢谢你的帮助 我有一个名为global_output的数据框架,它有两列:一列字和一列相应的值。我想对这些值进行中值分割,并根据它们是高于中值还是低于中值,将单词分配到两个列表中——高和低Python 为什么这个值显得模棱两可?,python,pandas,dataframe,Python,Pandas,Dataframe,我完全搞不懂为什么我在这段代码上会出现ValueError;谢谢你的帮助 我有一个名为global_output的数据框架,它有两列:一列字和一列相应的值。我想对这些值进行中值分割,并根据它们是高于中值还是低于中值,将单词分配到两个列表中——高和低 Word Ranking 0 shuttle 0.9075 1 flying 0.7750 2 flight 0.7250 3 trip
Word Ranking
0 shuttle 0.9075
1 flying 0.7750
2 flight 0.7250
3 trip 0.6775
4 transport 0.6250
5 escape 0.5850
6 trajectory 0.5250
7 departure 0.5175
8 arrival 0.5175
我的代码如下所示:
split = global_output['Abstraction'].quantile([0.5])
high = []
low = []
for j in range(len(global_output)):
if global_output['Ranking'][j] > split:
low_clt.append(global_output['Word'][j])
else:
high.append(global_output['Word'][j])
然而,我不断地得到这个错误
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
现在,我明白了错误的含义:它说我试图评估一个具有多个值的序列,就好像它是一个值一样。尽管如此,我还是看不出是怎么回事
global_output['Ranking'][j]
当j从循环中获取整数值时,在任何方面都是不明确的。当我将它输入控制台时,每次都会产生一个浮点值。我这里缺少什么?您使用的是
数组
,因此最好使用掩码
和loc
来选择列:
#if need column Abstraction, change it
split = global_output['Ranking'].quantile([0.5]).item()
print (split)
0.625
mask = global_output['Ranking'] <= split
print (mask)
0 False
1 False
2 False
3 False
4 True
5 True
6 True
7 True
8 True
Name: Ranking, dtype: bool
high = global_output.loc[~mask, 'Word'].tolist()
low = global_output.loc[mask, 'Word'].tolist()
print (high)
['shuttle', 'flying', 'flight', 'trip']
print (low)
['transport', 'escape', 'trajectory', 'departure', 'arrival']
你会得到
错误
,因为你比较了一个项目系列
啊,我明白了!模糊性来自全局输出[“排名]。分位数([0.5])。这很巧妙地解决了这个问题。谢谢你关于掩蔽的建议!确切地你是对的-需要比较两个标量。
split = global_output['Ranking'].quantile([0.5])
print (split)
0.5 0.625
Name: Ranking, dtype: float64
split = global_output['Ranking'].quantile([0.5]).item()
print (split)
0.625