无法将包含多列的数据框设置为单列。

huangapple go评论58阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月10日 22:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定