如何从数据框中做出决定?

时间:2019-06-30 18:18:01

标签: python dataframe dictionary

这是我的数据框:

  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

我需要将其转换成字典,所以我尝试了以下代码:

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

它为我回报8分

{'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']}

我只需要一个字典 任何帮助请

2 个答案:

答案 0 :(得分:3)

它没有给您返回八本字典,您只是将同一本词典打印了八次。每次您也对字典列表进行重设。

尽管如此,您无需自己制作字典,只需使用以下命令即可生成该字典:

df1.to_dict('list')

例如:

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

,或者当您要为每个 key (如'RefactoringType')值列表(如Detail)构造一个键时,可以使用groupby

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

例如:

>>> 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']}

答案 1 :(得分:2)

您可以尝试这样做,以保留原始结构:

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

输出:

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