Skip to content

Commit e5a41c0

Browse files
authored
feat: Export products as dataframe (#102)
1 parent 652f7c1 commit e5a41c0

File tree

6 files changed

+472
-15
lines changed

6 files changed

+472
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ._dataframe_utilities import convert_products_to_dataframe
12
from ._file_utilities import get_products_linked_to_file
23

34
# flake8: noqa
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List
2+
3+
import pandas as pd
4+
from nisystemlink.clients.product.models import Product
5+
from pandas import DataFrame
6+
7+
8+
def convert_products_to_dataframe(products: List[Product]) -> DataFrame:
9+
"""Converts a list of products into a normalized dataframe.
10+
11+
Args:
12+
products (List[Product]): A list of products
13+
14+
Returns:
15+
DataFrame:
16+
- A Pandas DataFrame containing the product data. The DataFrame would consist of all the
17+
fields in the input products.
18+
- A new column would be created for unique properties across all products. The property
19+
columns would be named in the format `properties.property_name`.
20+
"""
21+
products_dict_representation = [
22+
product.dict(exclude_none=True) for product in products
23+
]
24+
normalized_products_dataframe = pd.json_normalize(
25+
products_dict_representation, sep="."
26+
)
27+
28+
return normalized_products_dataframe

0 commit comments

Comments
 (0)