【问题标题】:creating dataframe from a function从函数创建数据框
【发布时间】:2022-01-01 12:01:05
【问题描述】:

我创建了以下函数来从参数创建数据框

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 个 Dataframe 1,但该函数将每个 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 个时,这给了我一个 Dataframes 列表

【问题讨论】:

  • 请提供一个最小可重复性示例
  • @mozway 检查编辑
  • 这是不可重现的,我无法在我的 python shell 中复制/粘贴 text 并获取要使用的对象;)
  • @mozway 立即查看,抱歉没有第一时间收到

标签: python-3.x pandas dataframe function mapping


【解决方案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

【讨论】:

  • 非常感谢,最后一件事,我可以从 line_ 中选择特定字符作为目标吗?
  • 另外,在 pd.concat 中传递一个函数和一个循环!我不知道这是可能的:D
  • @Kareem 不确定“从 line_ 中选择特定字符作为目的地”是什么意思,也许更新您的问题?或者更好的是,如果这足够不同,请打开一个新的后续问题。
  • 好的,我再开个问题谢谢
猜你喜欢
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-14
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多