API ValueError:值的长度与索引的长度不匹配

时间:2018-07-02 15:04:25

标签: python pandas api

我正在用python编写代码以从yelp api中提取评论数,价格,评分等信息。但是我收到一个值错误

def get_business_id(business):
    #import pdb; pdb.set_trace()
    try:
        business_id = search_matches(API_KEY, business)

        b_id = business_id['businesses'][0]['id']
    except:
        b_id = 'None'
    return b_id


df=pd.read_csv("ABC_sample10.csv", encoding='cp1252')
df['zip_code']=df['zip_code'].apply(str)

df1 = df.to_dict(orient='record') 

length = len(df1)
input_range = list(range(0, length)) 

def id_loop(df1):
    empty = []
    for i in input_range:
        business_id = get_business_id(df1[i])
        empty.append(business_id)
    return empty

a = id_loop(df1)


def get_details(business_id):
    empty_claimed = []
    empty_review = []
    empty_rating = []
    empty_price = []
    for i in input_range:
        business_detail = get_business(API_KEY,business_id[i])
        try:
            claimed_ind1 = business_detail['is_claimed']
            review_count_ind1 = business_detail['review_count']
            rated_ind1 = business_detail['rating']
            priced_ind1 = business_detail['price']
        except:
            claimed_ind1 = 'None'
            review_count_ind1 = 'None'
            rated_ind1 = 'None'
            priced_ind1 ='None'
        empty_claimed.append(claimed_ind1)
        empty_review.append(review_count_ind1)
        empty_review.append(rated_ind1)
        empty_review.append(priced_ind1)

    return empty_claimed, empty_review,empty_rating,empty_price 

b = get_details(a)



df['yelp_id']=a
df['claimed']=b[0]
df['review_count']=b[1]
df['rating']=b[2]
df['price']=b[3]
df.to_csv('sample10_ABCOutput8.csv')

我得到的错误是:

  

回溯(最近通话最近一次):

     

文件“”,行179,在       df ['review_count'] = b [1]

     

文件   “ C:\ Users \ rthuria \ AppData \ Local \ Continuum \ miniconda3 \ envs \ PythonData \ lib \ site-packages \ pandas \ core \ frame.py”,   第3116行,位于设置中       self._set_item(键,值)

     

文件   “ C:\ Users \ rthuria \ AppData \ Local \ Continuum \ miniconda3 \ envs \ PythonData \ lib \ site-packages \ pandas \ core \ frame.py”,   _set_item中的第3191行       值= self._sanitize_column(键,值)

     

文件   “ C:\ Users \ rthuria \ AppData \ Local \ Continuum \ miniconda3 \ envs \ PythonData \ lib \ site-packages \ pandas \ core \ frame.py”,   _sanitize_column中的第3388行       value = _sanitize_index(value,self.index,copy = False)

     

文件   “ C:\ Users \ rthuria \ AppData \ Local \ Continuum \ miniconda3 \ envs \ PythonData \ lib \ site-packages \ pandas \ core \ series.py”,   _sanitize_index中的第3998行       引发ValueError('值的长度与''index'的长度不匹配)

     

ValueError:值的长度与索引的长度不匹配

0 个答案:

没有答案