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) :
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 ?
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
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']}
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
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) :
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 ?
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
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']}
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