Home > database >  how can i change dataframe to dictionary
how can i change dataframe to dictionary

Time:08-31

import pandas as pd
data = {'articles': [{'site_category': '26','tags_string': 'aaa,bbb,ccc'}, 
                     {'site_category': '43','tags_string': 'ddd,eee,fff'},
                     {'site_category': '26','tags_string': 'aaa,hhh,iii'}]}
df = pd.json_normalize(data, 'articles')
print(df)

  site_category  tags_string
0            26  aaa,bbb,ccc
1            43  ddd,eee,fff
2            26  aaa,hhh,iii

how can i let him show this way?

    tags_string_list = [{"26":[{"aaa":2,"bbb":1,"ccc":1,"hhh":1,"iii":1}],
                         "43":[{'ddd': 1, 'eee': 1, 'fff': 1}],
                        }]

thanks for the reminder,I have corrected my expected output thank you very much

CodePudding user response:

As explained in this answer, you can use Counter to transform a list of values into a dictionary that has the counts of every item of that list:

from collections import Counter

result = (df['tags_string'].str.split(',')
                           .groupby(df['site_category'])
                           .agg( lambda col: dict(Counter(col.sum())) )
                           .to_dict()
)

result:

{'26': {'aaa': 2, 'bbb': 1, 'ccc': 1, 'hhh': 1, 'iii': 1},
 '43': {'ddd': 1, 'eee': 1, 'fff': 1}}

If you then want to have each value of a dictionary as a single element inside a list (or whatever other format you prefer) you can use a comprehension:

{k: [v] for k, v in result.items()}
{'26': [{'aaa': 2, 'bbb': 1, 'ccc': 1, 'hhh': 1, 'iii': 1}],
 '43': [{'ddd': 1, 'eee': 1, 'fff': 1}]}
  • Related