Groupby on pre-filtered values

Hello,

I aim to create a visualization in Kibana that mirrors the output obtained with the following Python pandas code. Please note that while the example code is in Python, my data resides within an Elasticsearch index.

import pandas as pd

# Assuming your dataframe is named df
data = {
    'a': [1, 1, 2, 2],
    'b': ['positive', 'negative', 'negative', 'negative'],
    'c': [5, 3, 3, 2]
}



df = pd.DataFrame(data)

# Filter rows where b is positive
positive_a = df[df['b'] == 'positive']['a']

# Filter rows where a is in positive_a
result = df[df['a'].isin(positive_a)].groupby('a')['c'].sum().reset_index()

print(result)

As long as all of these fields needed for this logic exist in a single document, you can use the Painless language to create runtime fields in the data, which act as plain fields you can use to filter the data.

See:

1 Like