InsideDarkWeb.com

How to make dict from dataframe?

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

Stack Overflow Asked by Oumayma Hamdi on November 22, 2021

2 Answers

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!

Related Questions

Android Preventing Double Click On A Layout with handher

1  Asked on January 12, 2021 by user13380366

     

How to center align the label text inside the option tag?

1  Asked on January 11, 2021 by mr-spock

         

How can I read the CSS for mobile images

0  Asked on January 11, 2021 by power-rico

   

Deleting from array using unset creates values

2  Asked on January 11, 2021 by cameron

 

passport.socket.io cant fix no session found

2  Asked on January 11, 2021 by edi_hadzic

         

Output should look like a quotation

4  Asked on January 11, 2021 by maurya

   

Sorting DateTime column in ASP.NET MVC

2  Asked on January 11, 2021 by steven85791

     

Rounding does not match all expected results

1  Asked on January 11, 2021 by aa-44

   

Django tabular input form (user can key in data into table)

1  Asked on January 11, 2021 by eka-buyung-lienadi

 

Seperate strings with regex and panda

1  Asked on January 11, 2021 by sara-daniel

         

Login into SAP using admin user fails

1  Asked on January 11, 2021 by gram77

 

Using LIKE with multiple columns in a table

3  Asked on January 10, 2021 by soldfor

   

Does the value of global variable persist in multiple API calls

2  Asked on January 10, 2021 by shubroto-shuvo

 

React Component not rendering to HTML

1  Asked on January 10, 2021 by red-apple

     

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.