ValueError:长度不匹配:预期轴包含0个元素,新值包含8个元素

时间:2019-04-27 13:25:36

标签: python python-3.x pandas

我有一个数据框,其中包含许多位置及其位置的详细信息,例如纬度和经度。我需要做的是使用map API请求每个地方的json文件以获取该地方附近的信息,然后打印出来。

一切工作都很好,直到我尝试创建一个函数来对数据的任何位置重复执行与我所做的相同的事情。

python3 熊猫

我可以分别在任何地方获得想要的东西,但是我的功能无法正常工作。

我搜索了一些有关同一类问题的线程,例如先更改列或首先创建新的数据框,但它们无济于事。

def get_nearby_venues(names, prices, latitudes, longitudes):
    venues_list=[]

    for name, price, lat, lng in zip(names, prices, latitudes, longitudes):
        print(name)

        # construct urls from page 1 to page 5(the first page will be displayed when page_num=0)
        url_list = []
        for page_num in range(0, 5):
            urls = 'http://api.map.baidu.com/place/v2/search?query=公园$超市$美食$学校$医院$公交车站$银行$电影院&location={},{}&radius=1000&output=json&scope=2&page_size=20&page_num='+str(page_num)+'&ak=(API key)'.format(lat, lng)
            url_list.append(urls)

        # make request to get json content
        results_json_list = []
        for each in url_list:
            results_json = requests.get(each).json()['results']

            # merge all pages json content into one file and all of my location data is stored in it.
            results_json_list.extend(results_json)



        # I try to use the following code to print out but failed.


        # return only relevant information for each nearby venue
        for each_item in results_json_list:
            venues_list.append([
                name, 
                price,
                lat, 
                lng, 
                each_item.get('name'), 
                each_item.get('location').get('lat'), 
                each_item.get('location').get('lng'),
                each_item.get('detail_info').get('type')])

    nearby_venues = pd.DataFrame([item for sublist in venues_list for item in sublist])
#     nearby_venues = pd.DataFrame(venues_list)

    nearby_venues.columns = ['Apartment', 
                             'Apartment Price',
                             'Apartment Latitude', 
                             'Apartment Longitude', 
                             'Venue', 
                             'Venue Latitude', 
                             'Venue Longitude', 
                             'Venue Category'] 

    return nearby_venues

# function code ends here


# dataframe data_venues is what I want to the results stored in for each location of my data and dataframe 'Data_map' is my previous dataframe which contains 'Name', 'Categories', 'Latitude',     'Longitude' columns of my data

data_venues = get_nearby_venues(names=Data_map['Name'],
                                prices=Data_map['Price'],
                                latitudes=Data_map['Latitude'],
                                longitudes=Data_map['Longitude']
                               )



ERROR MESSAGE code:
ValueError                                Traceback (most recent call last)
<ipython-input-33-9b269af7a350> in <module>
      8                                prices=Data_map.['Price'],
      9                                latitudes=Data_map['Latitude'],
---> 10                                longitudes=Data_map['Longitude']
     11                                )

<ipython-input-32-01b4632eb663> in get_nearby_venues(names, prices, latitudes, longitudes)
     44                              'Venue Latitude',
     45                              'Venue Longitude',
---> 46                              'Venue Category'] 
     47 
     48 

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py in __setattr__(self, name, value)
   5078         try:
   5079             object.__getattribute__(self, name)
-> 5080             return object.__setattr__(self, name, value)
   5081         except AttributeError:
   5082             pass

pandas/_libs/properties.pyx in pandas._libs.properties.AxisProperty.__set__()

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py in _set_axis(self, axis, labels)
    636 
    637     def _set_axis(self, axis, labels):
--> 638         self._data.set_axis(axis, labels)
    639         self._clear_item_cache()
    640 

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/internals/managers.py in set_axis(self, axis, new_labels)
    153             raise ValueError(
    154                 'Length mismatch: Expected axis has {old} elements, new '
--> 155                 'values have {new} elements'.format(old=old_len, new=new_len))
    156 
    157         self.axes[axis] = new_labels

ValueError: Length mismatch: Expected axis has 0 elements, new values have 8 elements

2 个答案:

答案 0 :(得分:0)

在已经创建好df的情况下已经只有一个列,您不能df.columns = list of more columns

在您获得nearby之后 插入以下代码,它将为正在发生的事情提供指导。但是并不能完全实现您想要的。

nearby = nearby.rename(
    columns=dict( zip(
        [0,1,2,3], 
        ['Apartment','Apartment Price', 'Apartment Latitude', 'Apartment Longitude'])) 
) 
for coln in ['Venue','Venue Latitude','Venue Longitude','Venue Category']:
    nearby.insert(column=coln, loc=len(nearby.columns), value=np.nan)   

该错误的根本原因是,新数据具有与旧数据相同的列数,这就是为什么旧方法适用于旧数据而不适用于新数据的原因数据。

答案 1 :(得分:0)

从以下位置更改URL格式后:

for page_num in range(0, 5):
       urls = 'http://api.map.baidu.com/place/v2/search?query=公园$超市$美食$学校$医院$公交车站$银行$电影院&location={},{}&radius=1000&output=json&scope=2&page_size=20&page_num='+str(page_num)+'&ak=(API key)'.format(
            lat,
            lng)

进入:

for num in range(0, 5):
    urls = 'http://api.map.baidu.com/place/v2/search?query=公园$超市$美食$学校$医院$公交车站$银行$电影院&location={},{}&radius=1000&output=json&scope=2&page_size=20&page_num={}&ak=(API key)'.format(
            lat, 
            lng,
            num)

有效。

我已经知道为什么我不能使用上一个请求json文件,这就是为什么它说期望的轴有0个元素

@ugn感谢您的见解:)