英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论