Reputation: 4798
I have a data set sort of like this:
fruits = ["orange", "plum", "lime"]
data = [(random.choice(fruits),
random.randint(0,100),
random.randint(0,100)) for i in range(16)]
dframe = pd.DataFrame(data, columns=["fruit", "x", "y"])
where fruit
has only a few values. I want a select widget so you can pick which kind of fruit you want to see in the plot.
Here's the update function I currently have:
source = bk.ColumnDataSource(dframe)
by_fruit = dframe.groupby('fruit')
def update(fruit):
grouped = by_fruit.get_group(fruit)
source.data['x'] = grouped['x']
source.data['y'] = grouped['y']
source.data['fruit'] = grouped['fruit']
source.push_notebook()
interact(update, fruit=fruits)
but going through and re-assigning the values of each column seems excessively verbose as I get more columns. It's error-prone, as if I leave out a column, they become different lengths and get misaligned.
Pandas excels at slicing and dicing things, and I feel like I'm missing something. What's a more concise way to change the Series
in each column of the ColumnDataSource
at the same time?
[This example in an IPython Notebook]
Upvotes: 0
Views: 1806
Reputation: 879471
You could iterate over the columns of grouped
:
def update(fruit):
grouped = by_fruit.get_group(fruit)
for col in grouped:
source.data[col] = grouped[col]
source.push_notebook()
Upvotes: 1