有沒有辦法在 pandas 中對(duì)字典進(jìn)行分組
嘗試按熊貓中的各大洲對(duì)國家/地區(qū)的小字典進(jìn)行分組。結(jié)果應(yīng)該是一個(gè)以大洲為索引的索引,以及第一列中的國家/地區(qū)數(shù)量。 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'}輸出應(yīng)該是這樣的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
查看完整描述