使用选择过滤散景中的数据帧并对过滤后的数据帧执行组操作

时间:2021-05-06 21:25:53

标签: python pandas bokeh

我正在使用 Bokeh 创建一个独立的 HTML 报告。我的主要数据来源是数据框。我已经找到了如何使用 CustomJS 回调更新表格或绘图。但是,我想使用 Select 小部件过滤原始数据帧,然后我想对过滤后的数据帧执行分组操作。 到目前为止,我无法弄清楚。例如:如果我的 df 数据框如下表所示:

<头>
ColA ColB ColC
A B 1
A B 1
C C 1

现在我想先选择 ColB='B' 然后按 ColA 分组的所有行
df[df['ColB']=='B'].groupby('ColA').agg({'ColC':'sum'}) 然后我会使用分组的 df 作为绘图或表格的来源。 提前致谢。

2 个答案:

答案 0 :(得分:0)

您不能在独立的 HTML 输出中使用真正的 Pandas 操作,因为那种输出在浏览器中只是 HTML 和 JavaScript,而浏览器对 Python 或 Pandas 一无所知。您有两个选择:

  • 使用 CustomJS 回调,并使用 JavaScript 代码手动进行任何分组,或

  • 部署一个 Bokeh Server application,让您可以使用真正的 Python 回调(例如,可以调用 Pandas 函数)

答案 1 :(得分:0)

对于独立的 HTML,您可以使用第三方 JS 库,例如 dataframe-js。通过这种方式,您可以将整个 Python 数据帧传递给 JS 并在 JS 中执行过滤和分组操作,就像您在 Python 中所做的那样。请参见下面的示例(针对 Bokeh v2.1.1 进行测试):

import os
import pandas as pd
from bokeh.io import save
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
from bokeh.util.browser import view

df = pd.DataFrame({'ColA': ['A', 'A', 'A'], 'ColB': ['B', 'B', 'C'], 'ColC': [1, 2, 3],'ColD': [5, 2, 5] })

template = """
{% block postamble %}
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script src="https://gmousse.github.io/dataframe-js/dist/dataframe.min.js"></script>
    
    <script>
        $(document).ready(function() {
            var DataFrame = dfjs.DataFrame
            var line = Bokeh.documents[0].get_model_by_name('my_line')
            var df = new DataFrame(line.data_source.data)
            
            console.log('printing original dataframe')
            df.show()
            
            groups = df.groupBy('ColB').toCollection()
            
            for (i = 0; i<groups.length; i++) {
                var group_tuple = groups[i]
        
                var name = group_tuple['groupKey']['ColB']
                var group = group_tuple['group']
                
                console.log('printing group ' + name)
                group.show()
            }
        });
    </script>
    
{% endblock %} """

source = ColumnDataSource(df)

p = figure()
p.line('ColC', 'ColD', source = source, name="my_line")
save(p, template=template)
view(os.path.join(os.path.dirname(__file__), os.path.basename(__file__)).replace('.py', ".html"))

JS 控制台输出:

printing original dataframe
| ColA      | ColB      | ColC      | ColD      | index     |
------------------------------------------------------------
| A         | B         | 1         | 5         | 0         |
| A         | B         | 2         | 2         | 1         |
| A         | C         | 3         | 5         | 2         |
​printing group B
| ColA      | ColB      | ColC      | ColD      | index     |
------------------------------------------------------------
| A         | B         | 1         | 5         | 0         |
| A         | B         | 2         | 2         | 1         |
​printing group C
| ColA      | ColB      | ColC      | ColD      | index     |
------------------------------------------------------------
| A         | C         | 3         | 5         | 2         |
相关问题