从函数创建数据框

creating dataframe from a function

我创建了以下函数来根据参数创建 Dataframe

gamle_dfs = []
def create_lines_df_2(Origin, Destination, line_, nodes_):
dict_ = [{'Origin':Origin,'Destination':Destination,'geometry':line_,
                 'length':line_.length,
               'osmid':[nodes_.index.values]}]
df = gpd.GeoDataFrame(dict_, geometry='geometry', 
crs=oslo_edges_proj.crs).reset_index()
gamle_dfs.append(df)

我将使用此函数恰好 289 次为每个区域路线设置 17 个 Dataframes 1,但是函数 returns 每个 Dataframe 作为列表的一个元素,我希望它们作为一个 Dataframe,如果我将列表更改为 GeoDataframe,它会给我一个空的 Dataframe,

结果是这样的:

    [       Origin  Destination                                           geometry  \
 0  Gamle Oslo  Grünerløkka  LINESTRING (599408.712 6642638.038, 599353.853...   
 
         length                                              osmid  
 0  1960.743326  [[1485390119, 79624, 1485390291, 24935363, 345...  ,
        Origin Destination                                           geometry  \
 0  Gamle Oslo      Sagene  LINESTRING (599408.712 6642638.038, 599353.853...   
 
         length                                              osmid  
 0  3799.280637  [[1485390119, 79624, 1485390291, 24935363, 345...  ]

我可以使用 gamle_dfs[0,.,.,n]

访问每个 Dataframe

将输出作为函数附加的 Dataframe 的解决方案是什么?

编辑添加示例:

origin = ['a']
destinations = ['b','c','d','e']
line1 = ['shaprely.geometry.nodes from a to b']
line2 = ['shaprely.geometry.nodes from a to c']
line3 = ['shaprely.geometry.nodes from a to d']
line4 = ['shaprely.geometry.nodes from a to e']


gamle_dfs = []

def create_lines_df_2test(Origin, Destination, line_):
    dict_ = 
    [{'Origin':Origin,'Destination':Destination,'geometry':line_,
    'length':len(line_)}]
    df = pd.DataFrame(dict_)
    gamle_dfs.append(df)

这给了我一个数据帧列表,当我只需要从那些 gamle_dfs 索引

中组合 1 个时

如果您真的需要在循环中生成数据帧,我会修改函数以输出数据帧,而不是更新全局变量。然后我将使用 pandas.concat 生成最终数据帧:

def create_lines_df_2test(Origin, Destination, line_):
    dict_ = [{'Origin':Origin,'Destination':Destination,'geometry':line_,
    'length':len(line_)}]
    df = pd.DataFrame(dict_)
    return df
    
lines = (line1, line2, line3, line4)
    
pd.concat([create_lines_df_2test(origin, destinations, l) for l in lines])

如果你一开始就有所有数据,直接生成dataframe即可:

df = pd.DataFrame({'Origin': [origin for x in range(len(lines))],
                   'Destination': [destinations for x in range(len(lines))],
                   'geometry': lines,
                   'length': map(len, lines),
                   })

输出:

  Origin   Destination                               geometry  length
0    [a]  [b, c, d, e]  [shaprely.geometry.nodes from a to b]       1
1    [a]  [b, c, d, e]  [shaprely.geometry.nodes from a to c]       1
2    [a]  [b, c, d, e]  [shaprely.geometry.nodes from a to d]       1
3    [a]  [b, c, d, e]  [shaprely.geometry.nodes from a to e]       1