-
Example Code# Pseudo code to illustrate what I'd like to do
from bigtree import dict_to_tree
path_dict_1 = {
"a": {"age": 90},
"a/b": {"age": 65},
"a/c": {"age": 60},
"a/b/d": {"age": 40},
"a/c/f": {"age": 38},
"a/b/e/h": {"age": 6},
}
path_dict_2 = {
"a": {"colour": "green"},
"a/b": {"colour": "pink"},
"a/c": {"colour": "ultraviolet"},
"a/b/d": {"colour": "cadmiumorange"},
"a/c/f": {"colour": "froggygreen"},
"a/b/e/h": {"colour": "wombatbrown"},
}
root_1 = dict_to_tree(path_dict_1)
root_2 = dict_to_tree(path_dict_2)
copy_nodes_from_tree_to_tree(
from_tree=root_1,
to_tree=root_2,
from_paths=["a"],
to_paths=["a"],
merge_children=True,
merge_attributes=True # <--- something like this would be desireable
)
root_2.show(attribute_list=['age', 'colour'])
>>> a [age=90, colour=green]
>>> ├── b [age=65, colour=pink]
>>> │ ├── d [age=40, colour=cadmiumorange]
>>> │ └── e
>>> │ └── h [age=6, colour=wombatbrown]
>>> └── c [age=6, colour=ultraviolet]
>>> └── f [age=38, colour=froggygreen] DescriptionHi - first off thanks for this excellent module! I have a question to which I was not able to find a simple answer for: is it possible to directly merge two identical tree structures (same nodes/hierarchies) which have different attributes in a way that the merged tree simply contains a merged/union of the attributes of both trees? I guess the other option would be to iterate over the two trees and construct a new merged tree or do this without Thanks! (edited initial typos) Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hello, thanks for the suggestion! In this scenario it will be more straightforward to convert the trees to dataframe, perform dataframe operation of merging, then converting it back to a tree because there is no 'shifting' or 'copying' involved if it is just to combine the attributes. Something like this - import pandas as pd
from bigtree import dict_to_tree, tree_to_dataframe, dataframe_to_tree
# Your provided input
path_dict_1 = {
"a": {"age": 90},
"a/b": {"age": 65},
"a/c": {"age": 60},
"a/b/d": {"age": 40},
"a/c/f": {"age": 38},
"a/b/e/h": {"age": 6},
}
path_dict_2 = {
"a": {"colour": "green"},
"a/b": {"colour": "pink"},
"a/c": {"colour": "ultraviolet"},
"a/b/d": {"colour": "cadmiumorange"},
"a/c/f": {"colour": "froggygreen"},
"a/b/e/h": {"colour": "wombatbrown"},
}
root_1 = dict_to_tree(path_dict_1)
root_2 = dict_to_tree(path_dict_2)
# Suggested processing
df1 = tree_to_dataframe(root_1, all_attrs=True)
df2 = tree_to_dataframe(root_2, all_attrs=True)
df_merge = pd.merge(df1, df2, on=["path", "name"])
root_merge = dataframe_to_tree(df_merge)
root_merge.show(all_attrs=True) This will result in the tree structure
Also, in your provided example I fixed some typos where it should be |
Beta Was this translation helpful? Give feedback.
Hello, thanks for the suggestion! In this scenario it will be more straightforward to convert the trees to dataframe, perform dataframe operation of merging, then converting it back to a tree because there is no 'shifting' or 'copying' involved if it is just to combine the attributes.
Something like this -