8000 add functions to extract leaf node data and convert to dataframe · larray-project/larray-editor@d8e1eee · GitHub
[go: up one dir, main page]

Skip to content

Commit d8e1eee

Browse files
committed
add functions to extract leaf node data and convert to dataframe
1 parent 9dee80b commit d8e1eee

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

larray_editor/editor.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ def indented_df_to_treenode(df, indent=4, indented_col=0, colnames=None, header=
118118
return root
119119

120120

121+
# Recursively traverse tree and extract the data *only* of leaf nodes
122+
def traverse_tree(node, rows):
123+
# If the node has no children, append its data to rows
124+
if not node.children:
125+
rows.append(node.data)
126+
# If the node has children, recursively call the function for each child
127+
for child in node.children:
128+
traverse_tree(child, rows)
129+
130+
131+
# Put all the leaf nodes of tree into a dataframe structure
132+
def tree_to_dataframe(root):
133+
rows = []
134+
traverse_tree(root, rows)
135+
return pd.DataFrame(rows, columns=root.data)
136+
137+
138+
121139
class FrequencyFilterDialog(QDialog):
122140
def __init__(self, available_labels, parent=None):
123141
super().__init__(parent)

0 commit comments

Comments
 (0)
0