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

基于参考字段值在SQL查询中生成条件新列(Python Pandas场景)

Conditional Column Selection in Pandas Based on ref_col Value

Got it, this is a common scenario in data manipulation with Pandas. Here are a few efficient ways to create your result_col based on the value of ref_col:

Method 1: Using numpy.where() (Most Efficient)

This is the go-to approach for simple conditional assignments—it’s vectorized, so it’s lightning fast even for large datasets.

import pandas as pd
import numpy as np

# Assume your DataFrame is named 'df' after querying the database
df['result_col'] = np.where(df['ref_col'] == 1, df['sent_time'], df['accept_time'])

How it works: If ref_col equals 1, we take the value from sent_time; otherwise, we use accept_time. Perfect for your two-value case.

Method 2: Using pandas.DataFrame.apply() (Flexible for Complex Logic)

If you might need to expand the logic later (e.g., add more conditions for other ref_col values), apply() with a lambda is easy to read:

df['result_col'] = df.apply(
    lambda row: row['sent_time'] if row['ref_col'] == 1 else row['accept_time'],
    axis=1
)

Note: This is less efficient than numpy.where() for very large DataFrames since it processes rows one by one, but it’s great for small to medium datasets or when you need more complex conditional logic.

Method 3: Using loc for Assignment

Another vectorized approach that’s easy to follow—we first set a default value, then overwrite the rows where ref_col is 1:

# Initialize result_col with accept_time values
df['result_col'] = df['accept_time'].copy()
# Overwrite rows where ref_col is 1 with sent_time
df.loc[df['ref_col'] == 1, 'result_col'] = df['sent_time']

This is also efficient and explicit, which can help with readability for someone new to your code.

Edge Case Handling

If there’s a chance ref_col has values other than 1 or 2, you can extend the logic to handle those. For example, using numpy.select() if you have multiple conditions:

conditions = [
    df['ref_col'] == 1,
    df['ref_col'] == 2,
    df['ref_col'].isin([3,4])  # Example of additional condition
]
choices = [
    df['sent_time'],
    df['accept_time'],
    pd.NaT  # Assign NaT (Not a Time) for other values
]

df['result_col'] = np.select(conditions, choices, default=pd.NaT)

This way, you can explicitly define how to handle any unexpected values in ref_col.

Content of the question originates from Stack Exchange, asked by Jack Walsh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:15:01