I have a dataframe with only one row which contains columns such as subscription_id_avg, subscription_id_std etc.., around 84 columns. Each column contains a mean and a standard deviation. I want to convert this dataframe to a nested dictionary such as
{"subscription_id" : {"avg": 0.36, "std": 1.5}}
How do I split the column names and convert it into a nested dictionary form?
CodePudding user response:
Here, _std
and _avg
are in every column name so column_name[:-4]
will give the desired property name. For example, subscription_id_avg[:-4]
will yeild subscription_id
. Using this, we can get the keys for dictionary.
dict = {}
for cl in df.columns:
key_name = cl[:-4]
if dict.get(key_name) is None:
dict[key_name] = {cl[-3:]: df[cl][0]}
else:
dict[key_name][cl[-3:]] = df[cl][0]
Here dict will be the required python dictionary. Hope this will help.
This will work only if _avg
and _std
are the only suffixes in column names.