Have to make a customized dataframe from a dict with multiple values

Question:

Please find below my input/output :

INPUT :

dico = {'abc': 'val1=343, val2=935',
'def': 'val1=95, val2=935',
'ghi': 'val1=123, val2=508'}

OUTPUT (desired) :

enter image description here

I tried with pd.DataFrame.from_dict(dico, index=dico.keys) but unfortunately I got an error.

TypeError: DataFrame.from_dict() got an unexpected keyword argument
‘index’

Do you have any suggestions please ?

Asked By: Stormy

||

Answers:

You can use DataFrame.from_records


In [44]: records = []
    ...: for key,vals in dico.items():
    ...:     vals = [ tuple(v.strip().split("=")) for v in vals.split(",")]
    ...:     records.append(dict(vals))
    ...: df = pandas.DataFrame.from_records(records, index=dico.keys())

In [45]: records
Out[45]:[{'val1': '343', 'val2': '935'},
 {'val1': '95', 'val2': '935'},
 {'val1': '123', 'val2': '508'}]

In [46]: df
Out[46]: 
    val1 val2
abc  343  935
def   95  935
ghi  123  508
Answered By: dermen

Try this :

import re

dd = {key: list(map(str, re.sub('vald=', '', value).replace(' ', '').split(','))) for key, value in dico.items()}

df = pd.DataFrame.from_dict(dd, orient='index', columns=['val1', 'val2'])
# Output :
print(df)

    val1 val2
abc  343  935
def   95  935
ghi  123  508
# Intermediates :
print(dd)

{'abc': ['343', '935'], 'def': ['95', '935'], 'ghi': ['123', '508']}
Answered By: abokey

Let’s use a regex pattern to find the matching pairs corresponding to each value in the input dictionary then convert the pairs to dict and create a new dataframe

import re

pd.DataFrame([dict(re.findall(r'(S+)=(d+)', v)) for k, v in dico.items()], dico)

Alternative pandas only approach with extractall (might be slower):

pd.Series(dico).str.extractall(r'(S+)=(d+)').droplevel(1).pivot(columns=0, values=1)

Result

    val1 val2
abc  343  935
def   95  935
ghi  123  508
Answered By: Shubham Sharma
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.