TransWikia.com

How to make dict from dataframe?

Stack Overflow Asked by Oumayma Hamdi on November 22, 2021

This is my Dataframe:

  RefactoringType Detail
0 Move Attribute com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment
1 Move Method com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment
2 Move Method com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment
3 Move Method ccom.sunlightlabs.android.congress.fragments.LegislatorProfileFragment
4 Move Method com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment
5 Move Method com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment
6 Move Method com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment
7 Move Method com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment

I need to transform it into a dictso I tried this code:

for i in range(df1.shape[0]):
   my_map[df1['Detail'][i]] = []
   my_map[df1['Detail'][i]].append(df1['RefactoringType'][i])
   print(my_map)

It return for me 8 ditcs

{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Attribute']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}

I need just one dict
Any help please

2 Answers

You can try this, in order to preserve original structure:

list_dicts = [[{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Attribute']},
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']},
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']},
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']},
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']},
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']},
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']},
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Method']}]

final_dict = {}

for _dict in list_dicts:
    for k, v in _dict.items():
        if k in final_dict:
            final_dict[k].append(v[0])
            continue
        final_dict[k] = v

Ouput:

{
   'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment':[
      'Move Attribute',
      'Move Method',
      'Move Method',
      'Move Method',
      'Move Method',
      'Move Method',
      'Move Method',
      'Move Method'
   ]
}

Answered by sashaboulouds on November 22, 2021

It did not return you eight dictionaries, you simply printed the same dictionary eight times. Each time you did a reset on the list of the dictionary as well.

Nevertheless, there is no need to produce the dictionary yourself, you can simply generate this with:

df1.to_dict('list')

For example:

>>> df
   foo  bar
0    1    4
1    2    5
>>> df.to_dict('list')
{'foo': [1, 2], 'bar': [4, 5]}

or when you want to construct a key per key (like 'RefactoringType') a list of values (like Detail), you can use a groupby:

{k: v.tolist() for k, v in df.groupby('RefactoringType')['Detail']}

For example:

>>> df
                                     RefactoringType          Detail
0  com.sunlightlabs.android.congress.fragments.Le...  Move Attribute
1  com.sunlightlabs.android.congress.fragments.Le...     Move Method
2  com.sunlightlabs.android.congress.fragments.Le...     Move Method
3  com.sunlightlabs.android.congress.fragments.Le...     Move Method
4  com.sunlightlabs.android.congress.fragments.Le...     Move Method
5  com.sunlightlabs.android.congress.fragments.Le...     Move Method
6  com.sunlightlabs.android.congress.fragments.Le...     Move Method
7  com.sunlightlabs.android.congress.fragments.Le...     Move Method
>>> {k: v.tolist() for k, v in df.groupby('RefactoringType')['Detail']}
{'com.sunlightlabs.android.congress.fragments.LegislatorProfileFragment': ['Move Attribute', 'Move Method', 'Move Method', 'Move Method', 'Move Method', 'Move Method', 'Move Method', 'Move Method']}

Answered by Willem Van Onsem on November 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP