2017-10-04 62 views
0

我有数据帧如何通过多列分组字段的总和排序数据帧

city  device sessions_count 
----------------------------- 
New York desktop 10 
New York mobile 9 
Chicago desktop 6 
Detroit desktop 16 
Detroit mobile 7 

我需要建立堆叠条形图,其中条代表的城市,它是由设备类型devided。我已成功地这样

city_device = df.groupby(['city', 'device'])['sessions_count'].agg([np.sum]); 
city_device.unstack().head(n=5).iplot(kind='bar', barmode='stack') 

使它只对第一行,但我需要只显示访问量最大的城市(排序sessions_count的总和每一个城市)。我怎样才能做到这一点?

+0

你想在移动/桌面之间进行拆分访问次数吗? –

+0

@StevenG是的,我需要保持这种分裂,但通过移动和桌面的总和 – cooperok

回答

1

我想你需要帮手列tmp进行排序,与0取代NaN■添加fill_value=0参数unstack

a = df.groupby(['city', 'device'])['sessions_count'].sum().unstack(fill_value=0) 
a = a.assign(tmp=a.sum(axis=1)).sort_values('tmp', ascending=False).drop('tmp', 1).head(5) 
print (a) 
device desktop mobile 
city      
New York  10  9 
Detroit  16  1 
Chicago   6  0 
+0

嗯,数据有问题?底特律移动应该是7? –

+0

提供的数据框中有一个错误,它是modile而不是mobile –

1

只是改变tail()值更大的一组:

grouped_all = df.groupby(['city']).sum() 
city = grouped_all.sort_values('sessions_count').tail(2).index 
grouped_split = df[df.city.isin(city)].groupby(['city', 'device']).sum() 

        sessions_count 
city  device     
Detroit desktop    16 
     mobile    7 
New York desktop    10 
     mobile    9 

now plot

grouped_split.unstack(level=0).plot.bar(stacked=True) 
1

我可以考虑的方法是使用带有边距的pivot_table,以按城市计算总值。然后,您可以按边距排序,删除边距,然后绘制堆叠的条形图。

下面的代码:

# Creates the DataFrame 
df = pd.DataFrame({ 
    'city':['New York', 'New York', 'Chicago', 'Detroit', 'Detroit'], 
    'device': ['desktop', 'mobile', 'desktop', 'desktop', 'mobile'], 
    'session_count': [10, 9, 6, 16, 7] 
}) 

# Creates a pivot table with margins named 'All' 
# Sorts by 'All' column 
# Drops the margins 
# Plots the stacked barplot 
df.pivot_table(columns='device', 
       index='city', 
       values='session_count', 
       aggfunc=sum, 
       margins=True).\ 
    sort_values(by='All', ascending=False).\ 
    drop('All').drop('All', axis=1).\ 
    plot.bar(stacked=True); 

而这里的结果:

Result chart

的 '一步一步' 的数据结构如下:

# Creates the DataFrame 
df = pd.DataFrame({ 
    'city':['New York', 'New York', 'Chicago', 'Detroit', 'Detroit'], 
    'device': ['desktop', 'mobile', 'desktop', 'desktop', 'mobile'], 
    'session_count': [10, 9, 6, 16, 7] 
}) 

print(df) 

#  city device session_count 
# 0 New York desktop    10 
# 1 New York mobile    9 
# 2 Chicago desktop    6 
# 3 Detroit desktop    16 
# 4 Detroit mobile    7 

print(df.pivot_table(columns='device', 
       index='city', 
       values='session_count', 
       aggfunc=sum, 
       margins=True)) 

# device desktop mobile All 
# city       
# Chicago  6.0  NaN 6.0 
# Detroit  16.0  7.0 23.0 
# New York  10.0  9.0 19.0 
# All   32.0 16.0 48.0 

print(df.pivot_table(columns='device', 
       index='city', 
       values='session_count', 
       aggfunc=sum, 
       margins=True).\ 
    sort_values(by='All', ascending=False)) 

# device desktop mobile All 
# city       
# All   32.0 16.0 48.0 
# Detroit  16.0  7.0 23.0 
# New York  10.0  9.0 19.0 
# Chicago  6.0  NaN 6.0 


print(df.pivot_table(columns='device', 
       index='city', 
       values='session_count', 
       aggfunc=sum, 
       margins=True).\ 
    sort_values(by='All', ascending=False).\ 
    drop('All').drop('All', axis=1)) 

# device desktop mobile 
# city      
# Detroit  16.0  7.0 
# New York  10.0  9.0 
# Chicago  6.0  NaN 

然后,所有你必须做的是绘制堆叠的barplot。

相关问题