尝试按熊猫中的各大洲对国家/地区的小字典进行分组。结果应该是一个以大洲为索引的索引,以及第一列中的国家/地区数量。 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'}输出应该是这样的Index Country Column1Asia 5United States 2Europe 6...不必按任何顺序排序到目前为止的代码countries_df = pd.DataFrame.from_dict(ContinentDict,orient='index') #columns=['size', 'sum', 'mean', 'std'] #countries_df = countries_df.rename(columns={0:"sampCol"}) #countries_df[columns[0]]=np.nan #countries_df[columns[1]]=np.nan #countries_df[columns[2]]=np.nan #countries_df[columns[3]]=np.nan #countries_df=countries_df.set_index('A').groupby(0) countries_df=countries_df.rename(index={" ":"Countries"}) #countries_df=countries_df.groupby('sampCol') #countries_df = countries_df.sum() #countries_df['size']=countries_df.groupby(['sampCol']).sum() return countries_df
2 回答
婷婷同学_
TA贡献1844条经验 获得超8个赞
pandas.Series.groupby与以下一起使用count:
s = pd.Series(ContinentDict, name="Country")
s.groupby(s).count()
输出:
Country
Asia 5
Australia 1
Europe 6
North America 2
South America 1
Name: Country, dtype: int64
动漫人物
TA贡献1815条经验 获得超10个赞
您可以使用
countries_df = pd.DataFrame(continentDict.items(), columns=('country', 'continent'))
countries_df.groupby('continent').count()
country
continent
Asia 5
Australia 1
Europe 6
North America 2
South America 1
添加回答
举报
0/150
提交
取消