首页 > 解决方案 > 我可以将 for 循环的输出转换为数据框中的列吗?

问题描述

我正在获取巴尔的摩社区的纬度和经度,并且有一个工作for循环列出了具有纬度和经度的社区名称,但需要将输出放入数据框中。

# Gets a list from excel spreadsheet
def readcolumn(filename,column):
    #select sheet name and selct column as index,index_col=0
    df = pd.read_excel(filename)
    headername = list(df)
    print(headername)
    column_data =df[list(df)[column]].tolist()
    return  column_data

# Converts the neighborhood column in the spreadsheet to a list
neigh_list = readcolumn('BaltimoreNeighborhoods.xlsx', 2)

# Outputs the neighborhood, lat, and long
for neigh in neigh_list:
    try:
        geolocator = Nominatim(user_agent="bmore_explorer")
        location = geolocator.geocode(neigh)
        latitude = location.latitude
        longitude = location.longitude
        print(neigh, latitude, longitude)
    except Exception as e:
        print('Error, skipping address...', e)

输出如下所示:

Arlington, MD 39.3486919 -76.6826661
Ashburton, MD 39.3279621 -76.6710811
Callaway-Garrison, MD 39.3321612 -76.6794359
Central Park Heights, MD 39.3444594 -76.6712351

我想将此列表转换为 df。

标签: pythonpandasfor-loop

解决方案


一种选择是构建矩阵,然后将其转换为 pandas DataFrame。

df = []
for neigh in neigh_list:
    try:
        geolocator = Nominatim(user_agent="bmore_explorer")
        location = geolocator.geocode(neigh)
        latitude = location.latitude
        longitude = location.longitude
        df.append([location, latitude, longitude])
    except Exception as e:
        print('Error, skipping address...', e)
df = pd.DataFrame(df, columns=['location', 'latitude', 'longitude'])

顺便说一句,pandas 还具有直接从 excel 表中读取的功能,这可以简化您的 readcolumn 功能


推荐阅读