You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Pandas高效实现基于前置值的列赋值操作?

Pandas向量化实现Event列状态转换的方向指引

针对你需要的转换逻辑,核心是利用Pandas的向量化映射和前向填充能力替代循环,以下是关键方向:

  • 第一步:处理明确映射关系
    用Series.replace()或np.where()直接将Event列中的Arrival、Departure转换为对应的At Sea/At Port,同时将其他值标记为缺失值(如NaN)。

  • 第二步:追踪最近前置触发事件
    单独提取Event列中的Arrival/Departure,其余值设为NaN,再用Series.ffill()(前向填充)为每一行匹配到最近的前置触发事件,快速完成参考事件的全局关联。

  • 第三步:处理非触发事件的映射
    基于前向填充得到的参考事件,用np.where()或Series.map()完成反转映射:参考事件为Arrival则映射为At Port,参考事件为Departure则映射为At Sea。

  • 第四步:合并结果
    将明确映射的结果与非触发事件的映射结果合并,最终生成目标列。

全程采用Pandas向量级操作,相比循环效率提升明显,尤其适配大数据量场景。

附你的循环实现代码:

import knime.scripting.io as knio
import pandas as pd

# Create a pandas data frame with the contents of the KNIME node input table
inputTable = knio.input_tables[0].to_pandas()

# Resetting index for the main table to integer from 0 to n
inputTable.reset_index(drop=True, inplace=True)

# Create a series out of the colummn Event
eventSeries = inputTable.loc[:, "Event"]

# Create a new series with "none" values to use for writing the Sea/Port info into
length = eventSeries.size
seaPortValueSeries = pd.Series("none", index=range(length))
seaPortValueSeries = seaPortValueSeries.astype(str)

# Iterate over the elements of the series, writing a value into a second
# series that contains only "At Sea" and "At Port" as entries.
# For the first iteration, it makes no sense to check predecessors
# or retrieve predecessor values, so a shortened version is used for that iteration
for i in range(length):

    if i == 0:
        
        if eventSeries[i] == "Arrival":
            seaPortValueSeries[i] = "At_Sea"
        if eventSeries[i] == "Departure":
            seaPortValueSeries[i] = "In_Port"
    else:

        if eventSeries[i] == "Arrival":
            seaPortValueSeries[i] = "At_Sea"
        elif eventSeries[i] == "Departure":
            seaPortValueSeries[i] = "In_Port"
        elif eventSeries[i-1] == "Arrival":
            seaPortValueSeries[i] = "In_Port"
        elif eventSeries[i-1] == "Departure":
            seaPortValueSeries[i] = "At_Sea"
        else:
            seaPortValueSeries[i] = seaPortValueSeries[i-1]

# Append the second series as an additional column into the data frame
inputTable["Sea_Port"] = seaPortValueSeries

knio.output_tables[0] = knio.Table.from_pandas(inputTable)

内容的提问来源于stack exchange,提问作者schuppius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 09:19:55