Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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 ?


2 Answers

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
like image 114
Shubham Sharma Avatar answered Oct 31 '25 06:10

Shubham Sharma


You can use DataFrame.from_records

records = []
for key,vals in dico.items():
    vals = [ tuple(v.strip().split("=")) for v in vals.split(",")]
    records.append(dict(vals))
#records:
#[{'val1': '343', 'val2': '935'},
# {'val1': '95', 'val2': '935'},
# {'val1': '123', 'val2': '508'}]

df = pandas.DataFrame.from_records(records, index=dico.keys())

#    val1 val2
#abc  343  935
#def   95  935
#ghi  123  508
like image 23
dermen Avatar answered Oct 31 '25 05:10

dermen