Вы можете использовать Pandas сторонних производителей для преобразования в фрейм данных с помощью pd.read_fwf
(«с фиксированной шириной»).Ваши данные грязные, вам может потребоваться написать логику для расчета ширины столбцов или добавить их вручную.С учетом входного словаря d
:
from io import StringIO
import pandas as pd
df = pd.read_fwf(StringIO(d['Logistic Regression']), widths=[30, 11, 10, 10, 10])\
.dropna().rename(columns={'Unnamed: 0': 'index'}).set_index('index')
print(df)
precision recall f1-score support
index
APAR Information 0.74 1.00 0.85 844.0
Affected Products and Versions 0.00 0.00 0.00 18.0
Answer 0.00 0.00 0.00 30.0
Applicable component levels 0.96 0.85 0.90 241.0
Error description 0.48 0.56 0.52 754.0
Local fix 0.89 0.03 0.06 266.0
Modules/Macros 0.96 0.87 0.91 326.0
Problem 0.00 0.00 0.00 63.0
Problem summary 0.51 0.73 0.60 721.0
Related information 0.00 0.00 0.00 22.0
Resolving The Problem 0.00 0.00 0.00 60.0
Temporary fix 0.00 0.00 0.00 32.0
circumvenion 0.00 0.00 0.00 124.0
component 0.00 0.00 0.00 49.0
temporary_fix 0.00 0.00 0.00 2.0
micro avg 0.64 0.64 0.64 3552.0
macro avg 0.30 0.27 0.26 3552.0
weighted avg 0.60 0.64 0.58 3552.0
Затем используйте словарь для понимания:
res = {'Logistic Regression': {idx: df.loc[idx].tolist() for idx in df.index}}
print(res)
{'Logistic Regression':
{'APAR Information': [0.74, 1.0, 0.85, 844.0],
'Affected Products and Versions': [0.0, 0.0, 0.0, 18.0],
'Answer': [0.0, 0.0, 0.0, 30.0],
'Applicable component levels': [0.96, 0.85, 0.9, 241.0],
'Error description': [0.48, 0.56, 0.52, 754.0],
'Local fix': [0.89, 0.03, 0.06, 266.0],
'Modules/Macros': [0.96, 0.87, 0.91, 326.0],
'Problem': [0.0, 0.0, 0.0, 63.0],
'Problem summary': [0.51, 0.73, 0.6, 721.0],
'Related information': [0.0, 0.0, 0.0, 22.0],
'Resolving The Problem': [0.0, 0.0, 0.0, 60.0],
'Temporary fix': [0.0, 0.0, 0.0, 32.0],
'circumvenion': [0.0, 0.0, 0.0, 124.0],
'component': [0.0, 0.0, 0.0, 49.0],
'macro avg': [0.3, 0.27, 0.26, 3552.0],
'micro avg': [0.64, 0.64, 0.64, 3552.0],
'temporary_fix': [0.0, 0.0, 0.0, 2.0],
'weighted avg': [0.6, 0.64, 0.58, 3552.0]}}