本文介绍了用散景绘制一个groupby对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下MWE.

from pandas import DataFrame
from bokeh.plotting import figure
data = dict(x = [0,1,2,0,1,2],
            y = [0,1,2,4,5,6],
            g = [1,1,1,2,2,2])
df = DataFrame(data)
p = figure()
p.line( 'x', 'y', source=df[ df.g == 1 ] )
p.line( 'x', 'y', source=df[ df.g == 2 ] )

理想情况下,我想将最后一行压缩成一行:

Ideally, I would like to compress the last to lines in one:

p.line( 'x', 'y', source=df.groupby('g') )

(现实生活中的示例具有大量可变的组.)是否有任何简洁的方法?

(Real life examples have a large and variable number of groups.) Is there any concise way to do this?

推荐答案

这是Tony的解决方案,略有简化.

This is Tony's solution slightly simplified.

import pandas as pd
from bokeh.plotting import figure
data = dict(x = [0, 1, 2, 0, 1, 2],
            y = [0, 1, 2, 4, 5, 6],
            g = [1, 1, 1, 2, 2, 2])
df = pd.DataFrame(data)
####################### So far as in the OP
gby = df.groupby('g')
p = figure()
x = [list( sdf['x'] ) for i,sdf in gby]
y = [list( sdf['y'] ) for i,sdf in gby]
p.multi_line( x, y )

这篇关于用散景绘制一个groupby对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 23:53