使用 defaultdict 对 pandas 中的值进行分类

我正在尝试使用以下词典(' ContinentDict ')按大陆对国家/地区进行分类。


因此,我想按值对键进行分类。


ContinentDict  = {'China':'Asia', 

                  'United States':'North America', 

                  'Japan':'Asia', 

                  'United Kingdom':'Europe', 

                  'Russian Federation':'Europe', 

                  'Canada':'North America', 

                  'Germany':'Europe', 

                  'India':'Asia',

                  'France':'Europe', 

                  'South Korea':'Asia', 

                  'Italy':'Europe', 

                  'Spain':'Europe', 

                  'Iran':'Asia',

                  'Australia':'Australia', 

                  'Brazil':'South America'}

当我尝试选项 1 时:


v = {}


for key, value in sorted(d.items()):

    v.setdefault(value, []).append(key)

我收到错误:


Traceback (most recent call last):

  File "<input>", line 2, in <module>

TypeError:'dict' object is not callable

当我尝试选项 2 时:


from collections import defaultdict


dictionary = defaultdict(list)

for key, value in ContinentDict:

dictionary[value].append(key)

我收到错误:


Traceback (most recent call last):

  File "<input>", line 1, in <module>

TypeError: first argument must be callable or None

有人可以帮我吗?


SMILET
浏览 104回答 1
1回答

动漫人物

对于选项 2,我想你错过了.items()。这对我有用:ContinentDict&nbsp; = {'China':'Asia',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'United States':'North America',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Japan':'Asia',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'United Kingdom':'Europe',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Russian Federation':'Europe',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Canada':'North America',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Germany':'Europe',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'India':'Asia',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'France':'Europe',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'South Korea':'Asia',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Italy':'Europe',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Spain':'Europe',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Iran':'Asia',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Australia':'Australia',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Brazil':'South America'}dictionary = defaultdict(list)for key, value in ContinentDict.items():&nbsp; &nbsp; dictionary[value].append(key)print(dictionary)输出:defaultdict(<class 'list'>, {'Asia': ['China', 'Japan', 'India', 'South Korea', 'Iran'], 'North America': ['United States', 'Canada'], 'Europe': ['United Kingdom', 'Russian Federation', 'Germany', 'France', 'Italy', 'Spain'], 'Australia': ['Australia'], 'South America': ['Brazil']})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python