python - python networkx允许重复节点
问题描述
我正在尝试查看 3 列并创建一个迭代树列表。
我想迭代到顶部,直到找到空父代码并显示完整的树视图。
我正在使用的代码:
df = df.fillna('null')
G = nx.DiGraph()
G.add_edges_from(zip(df['ChildCode'],df['parentCode'] ))
G = nx.relabel_nodes(G, mapping=df.set_index('ChildCode')['ChildOrg'].to_dict())
我对节点的重新标记是删除重复值,即财务,因此结果有点偏离。如果有办法在重新标记时允许重复节点?
由于某种原因,我得到的结果无法识别重复的 childorg 条目并将所有条目分配给相同的父代码。
我期待如下结果:
解决方案
If df['null']
contains numbers, then they are not subscriptable. For example, 123456[1:3]
would raise the same error. I guess you want the following code:
df['Path'] = df['null'].apply(lambda x: '/'.join(str(x)[-2::-1]))
Another potential problem is filling out the missing values by 'null'. Do you want 'un' to be returned in this case? If not, please treat these values separately. For example,
df['Path'] = df['null'].apply(lambda x: '/'.join(str(x)[-2::-1]) if x != 'null' else 'null')
推荐阅读
- hibernate-search - Hibernate Search 仅添加来自 IndexedEmbedded 类的 DocumentId
- iis - 在 windows server 2012 R2 的 iis 中创建自签名证书
- python - 根据列表列表中的十六进制地址对内部列表进行排序
- powershell - New-UnifiedGroup 不适用于 ErrorAction
- php - 我在 laravel 网站上收到错误 404
- javascript - 在Javascript中识别用户浏览器并传递给asp.net c#如果不是IE浏览器则显示错误页面
- perl - 如何避免在 perl 中产生非常小的计算错误
- redis - 我如何过滤 redis-cli 上的消息
- ruby-on-rails - 如何在 Ruby 中获取调用方法的名称?
- java - 使用Java中的集合从包含对象的列表中消除重复项