pandas 如何从 Bokeh ColumnDatasource 中提取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38693444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I extract data from a Bokeh ColumnDatasource
提问by multigoodverse
I was trying to avoid using a ColumnDataSource and instead of that I was passing pandas dataframe columns directly to Bokeh plots.
我试图避免使用 ColumnDataSource 而不是将Pandas数据框列直接传递给散景图。
Soon though I had to implement a HoverTool which requires to have the data in a ColumnDataSource. So, I started using ColumnDataSource.
很快,尽管我不得不实现一个 HoverTool,它需要在 ColumnDataSource 中包含数据。所以,我开始使用 ColumnDataSource。
Now, I was creating a box annotation and I had to use the maximum value of a certain column from my data to define the top border of the box.
现在,我正在创建一个框注释,我必须使用数据中某个列的最大值来定义框的顶部边框。
I can do that easily using pandas:
我可以使用Pandas轻松做到这一点:
low_box = BoxAnnotation(
top=flowers['petal_width'][flowers['species']=='setosa'].max(),
fill_alpha=0.1, fill_color='red')
But I can't figure out how to extract the maximum from a ColumnDataSource.
但我不知道如何从 ColumnDataSource 中提取最大值。
Is there a way to extract a maximum value from it, or is my approach all wrong in the first place?
有没有办法从中提取最大值,或者我的方法首先是错误的?
回答by benten
A ColumnDataSource object has an attribute data
which will return the python dictionary used to create the object in the first place.
ColumnDataSource 对象具有一个属性data
,该属性将首先返回用于创建对象的 Python 字典。
from bokeh.plotting import ColumnDataSource
# define ColumnDataSource
source = ColumnDataSource(
data=dict(
x=[1, 2, 3, 4, 5],
y=[2, 5, 8, 2, 7],
desc=['A', 'b', 'C', 'd', 'E'],
)
)
# find max for variable 'x' from 'source'
print( max( source.data['x'] ))
回答by InLaw
If the source input is a Pandas DataFrame, you can use the Standard method:
如果源输入是 Pandas DataFrame,则可以使用 Standard 方法:
source = ColumnDataSource(
data= pd.DataFrame( dict(
x=[1, 2, 3, 4, 5],
y=[2, 5, 8, 2, 7],
desc=['A', 'b', 'C', 'd', 'E'],
))
)
print( source.data['x'].max() )