Untitled
unknown
plain_text
20 days ago
904 B
2
Indexable
Never
def get_category_tree(df, category_dict, category_id): if category_id is np.nan: return '' tree = [] tree.insert(0, category_dict[category_id]) def get_tree(df, category_dict, category_id): parent_id = df[df['category_id'] == category_id]['parent_category_id'].values[0] if parent_id is np.nan: return ' -> '.join(tree) else: tree.insert(0, category_dict[parent_id]) return get_tree(df, category_dict, parent_id) return get_tree(df, category_dict, category_id) x_cat_df = pd.read_csv('~/Downloads/Shopify ICP categories - categories_raw.csv') x_categories = dict(zip(x_cat_df['category_id'], x_cat_df['category_name'])) def add_x_category_tree(row): return get_category_tree(x_cat_df, x_categories, row['category_id']) x_cat_df['category_tree'] = x_cat_df.apply(add_x_category_tree, axis=1)
Leave a Comment