1
我有以下一些行的数据帧: -用熊猫来访问元素的标量格式
restaurantName cuisine totalRating delivery
Bindia Indian Bistro indian 4.0 Yes
Bhoj Indian Cuisine indian 4.5 Yes
Indian Roti House indian 4.0 Yes
Utsav indian 4.0 Yes
姑且称之为DF3。现在,在Python控制台写以下工作: -
df3[df3.restaurantName == 'Bindia Indian Bistro'].totalRating.item()
,但是当我尝试使用读取文件如下这样做: -
for line in f1:
restName = line.strip('\n')
print(restName) # to check if restaurant name is read fine
overallToRest = df3[df3.restaurantName == restName].totalRating.item()
它提供了以下错误信息: -
Bindia Indian Bistro
Traceback (most recent call last):
File "<ipython-input-12-cdb8b6170a65>", line 18, in prepare_final_file
overallToRest = df3.loc[df3['restaurantName'] == restName]['totalRating'].item()
File "C:\Users\myName\Anaconda3\lib\site-packages\pandas\core\base.py", line 814, in item
return self.values.item()
ValueError: can only convert an array of size 1 to a Python scalar
我试图在Stack Overflow上搜索它,但找不到与我的问题相关的任何答案。请帮忙。提前致谢。
我试图实现此解决方案,但现在它给出了下面的错误: - '文件 “”,第24行,在prepare_final_file 打印(overallToRest.values [0]) IndexError:索引0超出轴0的大小为0' 我认为这是因为文件中可能有一些餐馆不在数据框中。为了纠正这个问题,我申请了尝试,例外块来跳过这些餐馆,但它仍然给出了同样的错误。 –
请检查编辑答案。 – jezrael
这个工作。谢谢 :) –