python plot过滤了groupby

时间:2017-07-17 07:29:36

标签: python pandas matplotlib seaborn

我有像这样的DataFrame

df = pd.DataFrame({
 'comments': {0: 0, 1: 1, 2: 47, 3: 102, 4: 230},
 'content_len': {0: 4305, 1: 7344, 2: 8431, 3: 5662, 4: 3706},
 'day': {0: 1, 1: 1, 2: 1, 3: 2, 4: 2},
 'dayofweek': {0: 2, 1: 2, 2: 2, 3: 3, 4: 3},
 'domain': {0: 'habrahabr.ru',
  1: 'habrahabr.ru',
  2: 'habrahabr.ru',
  3: 'habrahabr.ru',
  4: 'geektimes.ru'},
 'favs': {0: 0, 1: 1, 2: 72, 3: 36, 4: 6},
 'post_id': {0: 18284, 1: 18285, 2: 18286, 3: 18291, 4: 18294},
 'views': {0: 236, 1: 353, 2: 1200, 3: 5700, 4: 1400},
 'votes_minus': {0: 0.0, 1: 0.0, 2: 5.0, 3: 3.0, 4: 15.0},
 'votes_plus': {0: 0.0, 1: 1.0, 2: 45.0, 3: 72.0, 4: 73.0},
 'year_month': {0: datetime.strptime('2008-01-01', '%Y-%m-%d'),
  1: datetime.strptime('2008-01-01', '%Y-%m-%d'),
  2: datetime.strptime('2008-02-01', '%Y-%m-%d'),
  3: datetime.strptime('2008-02-01', '%Y-%m-%d'),
  4: datetime.strptime('2008-03-01', '%Y-%m-%d'),}})

现在我想绘制按'year_month'分组的不同图形,每个域一个图形。

例如文章数量

df[df.domain=='habrahabr.ru'].groupby('year_month').count()[['domain']].rename(columns={'domain':'habrahabr.ru'}).join(
df[df.domain=='geektimes.ru'].groupby('year_month').count()[['domain']].rename(columns={'domain':'geektimes.ru'})).plot()

或表示content_len

df[df.domain == 'habrahabr.ru'].groupby('year_month').mean()[['content_len']].rename(columns={'content_len':'habrahabr.ru'}).astype(int).join(
df[df.domain == 'geektimes.ru'].groupby('year_month').mean()[['content_len']].rename(columns={'content_len':'geektimes.ru'}).astype(int)).plot()

是否有比我给出的更优雅的解决方案?

1 个答案:

答案 0 :(得分:2)

所有域名的解决方案

我认为您可以在groupby函数中添加新列,然后按unstack重新整形:

What is the difference between size and count in pandas?

a = df.groupby(['year_month', 'domain']).size().unstack(fill_value=0)
print (a)
domain      geektimes.ru  habrahabr.ru
year_month                            
2008-01-01             0             2
2008-02-01             0             2
2008-03-01             1             0

a.plot()

也可以按summean ...

汇总
b = df.groupby(['year_month', 'domain'])['content_len'].mean().unstack(fill_value=0)
print (b)
domain      geektimes.ru  habrahabr.ru
year_month                            
2008-01-01           0.0        5824.5
2008-02-01           0.0        7046.5
2008-03-01        3706.0           0.0

b.plot()

另一个有点慢的解决方案是pivot_table

a = df.pivot_table(index='year_month', columns='domain', aggfunc='size', fill_value=0)
print (a)
domain      geektimes.ru  habrahabr.ru
year_month                            
2008-01-01             0             2
2008-02-01             0             2
2008-03-01             1             0


b = df.pivot_table(index='year_month', 
                   columns='domain', 
                   values='content_len', 
                   aggfunc='mean', 
                   fill_value=0)
print (b)
domain      geektimes.ru  habrahabr.ru
year_month                            
2008-01-01             0        5824.5
2008-02-01             0        7046.5
2008-03-01          3706           0.0

过滤域名的解决方案

如果只需要过滤某些域,请使用boolean indexingisin进行掩码或query

df1 = df[df['domain'].isin(['habrahabr.ru','geektimes.ru'])]
a = df1.groupby(['year_month', 'domain']).size().unstack(fill_value=0)
print (a)
domain      geektimes.ru  habrahabr.ru
year_month                            
2008-01-01             0             2
2008-02-01             0             2
2008-03-01             1             0
df1 = df.query('domain == ["habrahabr.ru", "geektimes.ru"]')
a = df1.groupby(['year_month', 'domain']).size().unstack(fill_value=0)
print (a)
domain      geektimes.ru  habrahabr.ru
year_month                            
2008-01-01             0             2
2008-02-01             0             2
2008-03-01             1             0