创建一个新的数据框列,使用其他列的数据,并根据条件进行操作。

huangapple go评论69阅读模式
英文:

create a new dataframe column using the data from other column on condition

问题

我想创建一个名为_faker_method_的列,该列将包含经过特殊转换的数据,通过检查Override Expression是否有值并使用它,如果有值的话,或者如果没有值的话,使用Default Expression列。

我尝试这样做:

df['_faker_invocation_'] = df['Override Expression'].apply(lambda x: render_faker_expression(df['Name'], x) if x else df['Default Expression'])

但是日志显示render_faker_expression函数接收到整个带有索引的列(因此在我的应用程序中失败)。

如何执行我需要的操作?

英文:

I have a data frame that looks like this

    Name        Default Expression    Override Expression 
0   AACT_NAM        pystr                   pyint
1   ACCT_CCY        pystr
2   ACCT_TYP        pystr

I want to create a column, _faker_method_ that would contain specially transformed data by checking if Override Expression has value and use it, if there is some value or
use Default Expression column if there is no value.

I tried to do like this

df['_faker_invocation_'] = df['Override Expression'].apply(lambda x: render_faker_expresison(df['Name'], x) if x else df['Default Expression'])

But logs should me that the function render_faker_expression recieves whole column with index (and thus it fails in my app)

0                           ACCT_NAM
1                           ACCT_CCY
2                           ACCT_TYP

How to perform the action I need?

答案1

得分: 1

以下是翻译好的内容:

您没有分享 `render_faker_expression`(或者至少它的签名),这使得很难确定问题所在但由于它似乎期望标量值而不是系列我猜想您想要逐行应用 lambda 函数

df['_faker_invocation_'] = df.apply(lambda row: render_faker_expresison(row['Name'], row['Override Expression'])
                                                if row['Override Expression']
                                                else row['Default Expression'], axis=1)

使用以下虚拟函数:

def render_faker_expresison(n, x):
    return x + "_fake_" + n

这给出了以下结果:

        Name Default Expression Override Expression   _faker_invocation_
ID                                                                      
0   AACT_NAM              pystr               pyint  pyint_fake_AACT_NAM
1   ACCT_CCY              pystr                None                pystr
2   ACCT_TYP              pystr                None                pystr
英文:

You're not sharing render_faker_expression (or at least its signature) which makes it hard to pin down the problem. But since it seems to expect a scalar value instead of a series, I guess you want to apply your lambda function row-wise:

df['_faker_invocation_'] = df.apply(lambda row: render_faker_expresison(row['Name'], row['Override Expression'])
                                                if row['Override Expression']
                                                else row['Default Expression'], axis=1)

With the following dummy function:

def render_faker_expresison(n, x):
    return x + "_fake_" + n

This gives us:

        Name Default Expression Override Expression   _faker_invocation_
ID                                                                      
0   AACT_NAM              pystr               pyint  pyint_fake_AACT_NAM
1   ACCT_CCY              pystr                None                pystr
2   ACCT_TYP              pystr                None                pystr

huangapple
  • 本文由 发表于 2023年2月26日 21:10:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定