I have the following DataFrame. I am wondering whether it is possible to break the data
column into multiple columns. E.g., from this:
ID Date
df = pd.DataFrame([
[6, "a: 1, b: 2"],
[6, "a: 1, b: 2"],
[6, "a: 1, b: 2"],
[6, "a: 1, b: 2"],
], columns=['ID', 'dictionary'])
def str2dict(s):
split = s.strip().split(',')
d = {}
for pair in split:
k, v = [_.strip() for _ in pair.split(':')]
d[k] = v
return d
df.dictionary.apply(str2dict).apply(pd.Series)
Or:
pd.concat([df, df.dictionary.apply(str2dict).apply(pd.Series)], axis=1)