2022-07-20 13:35:06 +02:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import pandas as pd
|
2022-08-09 18:36:40 +02:00
|
|
|
from tqdm.notebook import tqdm
|
2022-07-20 13:35:06 +02:00
|
|
|
|
|
|
|
|
2022-08-09 18:36:40 +02:00
|
|
|
def row_to_type(row, type, **type_kwargs):
|
|
|
|
return type(**row, **type_kwargs)
|
2022-07-20 13:35:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def df_to_type_list(df, type, **type_kwargs):
|
|
|
|
has_idx = not isinstance(df.index, pd.RangeIndex)
|
2022-08-09 18:36:40 +02:00
|
|
|
ret_list = []
|
|
|
|
p = tqdm(desc=" ├─ Processing", total=len(df))
|
|
|
|
for row in df.itertuples(index=has_idx):
|
|
|
|
p.update()
|
|
|
|
row_dict = row._asdict()
|
|
|
|
if has_idx:
|
|
|
|
row_dict["id"] = row.Index
|
|
|
|
del row_dict["Index"]
|
|
|
|
ret_list.append(row_to_type(row_dict, type, **type_kwargs))
|
|
|
|
return ret_list
|
2022-07-20 13:35:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def by_index(df, index, type):
|
|
|
|
return df_to_type_list(df.loc[index], type)
|
|
|
|
|
|
|
|
|
|
|
|
def by_column(df, column_name, column_val, type):
|
|
|
|
return df_to_type_list(df[df[column_name] == column_val], type)
|
|
|
|
|
|
|
|
|
|
|
|
def list_to_dict(ls, key='id'):
|
|
|
|
return {getattr(item, key): item for item in ls}
|