2017-11-25 78 views
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上搜索它,但找不到与我的问题相关的任何答案。请帮忙。提前致谢。

回答

0

restaurantName列中存在问题是重复的,所以筛选不仅返回一个值,而且还会返回更多值。

因此,可以只选择一个值来标量,例如,首先由[0],然后检查是否不emptySeriesif-else

overallToRest = df3.loc[df3.restaurantName == restName, 'totalRating'] 

overallToRest = 'no match' if overallToRest.empty else overallToRest.iat[0] 

另一种解决方案是drop_duplicates第一:

df3 = df3.drop_duplicates('restaurantName') 
overallToRest = df3.loc[df3.restaurantName == restName, 'totalRating'] 

overallToRest = 'no match' if overallToRest.empty else overallToRest.item() 

样品:

print (df3) 
     restaurantName cuisine totalRating delivery 
0 Bindia Indian Bistro indian   4.0  Yes 
1 Bindia Indian Bistro indian   4.5  Yes 
2  Indian Roti House indian   4.0  Yes 
3     Utsav indian   4.0  Yes 



restName = 'Bindia Indian Bistro' 

overallToRest = df3.loc[df3.restaurantName == restName, 'totalRating'] 
print (overallToRest) 
0 4.0 
1 4.5 
Name: totalRating, dtype: object 

print (overallToRest.item()) 

ValueError: can only convert an array of size 1 to a Python scalar

overallToRest = 'no match' if overallToRest.empty else overallToRest.item() 
print (overallToRest) 
4.0 

restName = 'Bindia Indian Bistro' 

df3 = df3.drop_duplicates('restaurantName') 
print (df3) 
     restaurantName cuisine totalRating delivery 
0 Bindia Indian Bistro indian   4.0  Yes 
2  Indian Roti House indian   4.0  Yes 
3     Utsav indian   4.0  Yes 

overallToRest = df3.loc[df3.restaurantName == restName, 'totalRating'] 
print (overallToRest) 
0 4.0 
Name: totalRating, dtype: float64 

overallToRest = 'no match' if overallToRest.empty else overallToRest.item() 
print (overallToRest) 
4.0 
+0

我试图实现此解决方案,但现在它给出了下面的错误: - '文件 “”,第24行,在prepare_final_file 打印(overallToRest.values [0]) IndexError:索引0超出轴0的大小为0' 我认为这是因为文件中可能有一些餐馆不在数据框中。为了纠正这个问题,我申请了尝试,例外块来跳过这些餐馆,但它仍然给出了同样的错误。 –

+0

请检查编辑答案。 – jezrael

+0

这个工作。谢谢 :) –