猿问

如何在使用多列和列名的 Pandas 中编写 lambda 表达式?

我想使用 Pandas lambda 表达式实现以下目标

  1. 在名称位于“ideal_prime_fc”列中的列中查找值

  2. 在名称位于“prime_fc”列中的列中查找值

  3. 找到差异并放入一个新列“delta”

慕尼黑8549860
浏览 211回答 1
1回答

万千封印

以下内容对您有用吗?编写一个函数来减去两个质数列中的值:def get_col_name(x):    try:        ip_fc = x['ideal_prime_fc']        p_fc = x['prime_fc']        return x[ip_fc]-x[p_fc]    except IndexError:        return float('NaN')  # handle non-existent values however you'd prefer应用函数,分配给一个新列:df['diff'] = df.apply(lambda x: get_col_name(x), axis=1)截断的示例输出:983     976     ideal_prime_fc  prime_fc    diff2835    780     973             805         NaN8       2259    983             983         0.02851    796     973             805         NaN13      7       983             976         6.0   # added for test
随时随地看视频慕课网APP

相关分类

Python
我要回答