Python pandas.DataFrame.loc两次给出标签的输出,并提到名称和数据类型
我有一个按项目id(数字)索引的数据框,我尝试使用Python pandas.DataFrame.loc两次给出标签的输出,并提到名称和数据类型,python,pandas,Python,Pandas,我有一个按项目id(数字)索引的数据框,我尝试使用DataFrame.loc[str(项目id),word]访问某个位置的项目,其中word是我要访问的列的标签。Word是一个字符串: print(output.loc[str(df.loc[i,'item_id']),word]) 我得到的输出如下: hu 0.0 hu 0.0 Name: 17349826, dtype: float64 这里的单词有值hu和item\u idhad17349826 我希望输出为: 0.0 我
DataFrame.loc[str(项目id),word]
访问某个位置的项目,其中word是我要访问的列的标签。Word是一个字符串:
print(output.loc[str(df.loc[i,'item_id']),word])
我得到的输出如下:
hu 0.0
hu 0.0
Name: 17349826, dtype: float64
这里的单词有值hu
和item\u id
had17349826
我希望输出为:
0.0
我认为您需要为标量输出选择
Series
by的第一个值:
print(output.loc[str(df.loc[i,'item_id']),word]).iat[0]
重复输出的原因是
索引
值中的重复。我认为您需要打印(output.loc[str(df.loc[I,'item_id']),word。).item()
用于将一个元素序列转换为标量值错误:只能将大小为1的数组转换为Python标量。出现此错误,因为正如您所看到的,它为我提供了hu twiceSo的值,以便使用print(output.loc[str(df.loc[i,'item_id')),word]).iat[0]
仅用于选择第一个值。谢谢@jezrael!!!你很快就帮了我。它工作得很好。但是你能告诉我为什么我一开始就得到2个值吗?看起来是重复的索引值。