我有一个这样的数据帧
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'),}})
现在我想绘制不同的图形分组'年\月',一个图形每个领域。你知道吗
例如文章数量
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()
或平均含量
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()
有比我给出的更优雅的解决方案吗?你知道吗
所有域的解决方案:
我认为您可以在} 进行重塑:
groupby
函数中添加新列,然后通过^{What is the difference between size and count in pandas?
也可以通过
sum
,mean
聚合。。。你知道吗另一个稍微慢一点的解决方案是^{} :
过滤域的解决方案:
如果需要筛选,则仅某些域使用^{} 和^{} 作为boolen掩码或^{} :
相关问题 更多 >
编程相关推荐