Please find below my input/output :
dico = {'abc': 'val1=343, val2=935',
'def': 'val1=95, val2=935',
'ghi': 'val1=123, val2=508'}

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 ?
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)
val1 val2
abc 343 935
def 95 935
ghi 123 508
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With