1
0
Fork 0
mirror of https://github.com/qurator-spk/modstool.git synced 2025-08-16 21:19:52 +02:00

🧹 Extract a function to convert list[dict] to a DataFrame

This commit is contained in:
Mike Gerber 2023-11-23 15:00:06 +01:00
parent 5c2dfa8505
commit 968572168e
2 changed files with 27 additions and 10 deletions

View file

@ -1,8 +1,9 @@
from itertools import groupby
import re
import warnings
from typing import List, Sequence, MutableMapping
from typing import List, Sequence, MutableMapping, Dict
import pandas as pd
import numpy as np
from lxml import etree as ET
@ -298,3 +299,26 @@ def flatten(d: MutableMapping, parent='', separator='_'):
return dict(items)
def dicts_to_df(data_list: List[Dict], *, index_column: str) -> pd.DataFrame:
"""
Convert the given list of dicts to a Pandas DataFrame.
The keys of the dicts make the columns.
"""
# Build columns from keys
columns = []
for m in data_list:
for c in m.keys():
if c not in columns:
columns.append(c)
# Build data table
data = [[m.get(c) for c in columns] for m in data_list]
# Build index
index = [m[index_column] for m in data_list]
df = pd.DataFrame(data=data, index=index, columns=columns)
return df