使用列数据源如何获取行的索引?

Using a column data source how can I get a row's index?

我正在使用从 CSV 文件填充的 pandas 数据框,然后我使用 Bokeh 将该数据框转换为 ColumnDataSource。

看起来像:

dataFrame = pandas.read_csv('somefile.CSV')
source = ColumnDataSource(dataFrame)

现在我有了所有的列,我想进行基于行的计算。

例如:我有三列:

x, y, colour

它可能填充了:

1, 2, blue
2, 5, red
1, 8, yellow

现在,我想在搜索源代码时更改该行中的一些关联变量,那么我该怎么做:

# how do i step through the source dictionary?
if source['colour'] == 'blue':
    # how do I get the current index, which is the row number
    # how do I change the x column value at the index(row) we retrieved
    source['x' index] = 2 

谢谢

如果您要遍历数据,您可以这样做:

dataFrame = pandas.read_csv('somefile.csv')
source = ColumnDataSource(dataFrame)

for index, colour in enumerate(source.data['colour']):
    if colour == 'blue':
        source.data['x'][index] = 2

或者,为了避免遍历整个 ColumnDataSource,您可以使用以下方法获取 'colour' 列中 'blue' 的第一个值的索引:

list(source.data['colour']).index('blue')

您可以将其用作编辑列 x 的索引,如下所示:

source.data['x'][list(source.data['colour']).index('blue')] = 2

以这种方式索引此列表只会为您提供值 'blue' 的第一个索引。如果您的 ColumnDataSource 中出现不止一次 'blue',应该为其编辑关联的 'x' 值,您应该能够通过索引列表之后的列表来遍历 'colour''blue' 的最后索引:

list(source.data['colour'])[last_index+1:].index('blue')

当它搜索的列表不包含值 'blue' 时,index('blue') 会抛出 ValueError,因为 index('blue') 应该将其所在的循环包装在 try 语句中。

使用

source.x[source.color == 'blue'] = 2

source.x 是您要更改的系列,括号中的条件 select 仅适用于它的行。