英文:
Cannot set a DataFrame with multiple columns to the single column
问题
我是中文翻译,以下是翻译好的部分:
我是新手使用pandas,我尝试修复我的脚本的最后部分。当调用以下函数时,我收到以下错误:
ValueError: 无法将多列的DataFrame设置到单列的place_name
def get_place_name(latitude, longitude):
location = geolocator.reverse(f"{latitude}, {longitude}", exactly_one=True)
if location is None:
return None
else:
return location.address
该函数从这里调用:
metadata_df = extract_metadata(dir_path)
print("[+] 从所有图像文件提取的元数据")
print(metadata_df.columns)
geolocator = Nominatim(user_agent="exif_location")
metadata_df['place_name'] = metadata_df.apply(
lambda row: get_place_name(
row['gps_latitude'], row['gps_longitude']),
axis=1)
任何帮助或建议将不胜感激
英文:
I am new to using pandas and I'm attempting to fix the last portion of my script. When calling the following function I am getting the error:
ValueError: Cannot set a DataFrame with multiple columns to the single column place_name
def get_place_name(latitude, longitude):
location = geolocator.reverse(f"{latitude}, {longitude}", exactly_one=True)
if location is None:
return None
else:
return location.address
This function is being called from here:
metadata_df = extract_metadata(dir_path)
print("[+] Metadata extracted from all image files")
print(metadata_df.columns)
geolocator = Nominatim(user_agent="exif_location")
metadata_df['place_name'] = metadata_df.apply(
lambda row: get_place_name(
row['gps_latitude'], row['gps_longitude']),
axis=1)
Any help or suggestions would be much appreciated
答案1
得分: 1
Your code works well for me (Pandas 1.5.3, Geopy 2.3.0):
df = pd.DataFrame({'lat': [34.053691, 40.712728],
'lon': [-118.242766, -74.006015]})
geolocator = Nominatim(user_agent='MyApp')
df['place_name'] = df.apply(lambda x: get_place_name(x['lat'], x['lon']), axis=1)
print(df)
Output
lat lon place_name
0 34.053691 -118.242766 Los Angeles City Hall, 200, North Spring Stree...
1 40.712728 -74.006015 New York City Hall, 260, Broadway, Lower Manha...
I don't understand why your function tries to return a dataframe with multiple columns. Your error is reproducible if you do something like:
df['place_name'] = df[['lat', 'lon']]
...
ValueError: Cannot set a DataFrame with multiple columns to the single column place_name
英文:
Your code works well for me (Pandas 1.5.3, Geopy 2.3.0):
df = pd.DataFrame({'lat': [34.053691, 40.712728],
'lon': [-118.242766, -74.006015]})
geolocator = Nominatim(user_agent='MyApp')
df['place_name'] = df.apply(lambda x: get_place_name(x['lat'], x['lon']), axis=1)
print(df)
Output
lat lon place_name
0 34.053691 -118.242766 Los Angeles City Hall, 200, North Spring Stree...
1 40.712728 -74.006015 New York City Hall, 260, Broadway, Lower Manha...
I don't understand why your function try to return a dataframe with multiple columns. Your error is reproducible if you do something like:
df['place_name'] = df[['lat', 'lon']]
...
ValueError: Cannot set a DataFrame with multiple columns to the single column place_name
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论