-
-
Notifications
You must be signed in to change notification settings - Fork 252
/
dict_to_table.py
54 lines (45 loc) · 1.64 KB
/
dict_to_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pandas as pd
import json
from loguru import logger
def dict_to_dataframe(data: dict) -> pd.DataFrame:
"""
Converts a dictionary into a Pandas DataFrame with formatted values.
Handles non-serializable values gracefully by skipping them.
Args:
data (dict): The dictionary to convert.
Returns:
pd.DataFrame: A DataFrame representation of the dictionary.
"""
formatted_data = {}
for key, value in data.items():
try:
# Attempt to serialize the value
if isinstance(value, list):
# Format list as comma-separated string
formatted_value = ", ".join(
str(item) for item in value
)
elif isinstance(value, dict):
# Format dict as key-value pairs
formatted_value = ", ".join(
f"{k}: {v}" for k, v in value.items()
)
else:
# Convert other serializable types to string
formatted_value = json.dumps(
value
) # Serialize value to string
formatted_data[key] = formatted_value
except (TypeError, ValueError) as e:
# Log and skip non-serializable items
logger.warning(
f"Skipping non-serializable key '{key}': {e}"
)
continue
# Convert the formatted dictionary into a DataFrame
return pd.DataFrame(
list(formatted_data.items()), columns=["Key", "Value"]
)
example = dict_to_dataframe(data={"chicken": "noodle_soup"})
# formatter.print_panel(example)
print(example)