How to convert dataframe columns which contains specific string to each columns to a nested dictionary?

Question:

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?

Asked By: kate22

||

Answers:

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.

Answered By: Shashi Kant
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.