Python 通过在两个现有列上使用 lambda 函数在 Panda 中创建一个新列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33680666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:46:20  来源:igfitidea点击:

Creating a new column in Panda by using lambda function on two existing columns

pythonpandaslambdamultiple-columnscalculated-columns

提问by piyush sharma

I am able to add a new column in Panda by defining user function and then using apply. However, I want to do this using lambda; is there a way around?

我可以通过定义用户函数然后使用 apply 在 Panda 中添加一个新列。但是,我想使用lambda来做到这一点;有办法解决吗?

For Example, dfhas two columns aand b. I want to create a new column cwhich is equal to the longest length between aand b.

例如,df有两列ab。我想创建一个新列c,它等于a和之间的最长长度b

Some thing like:

就像是:

df['c'] = df.apply(lambda x, len(df['a']) if len(df['a']) > len(df['b']) or len(df['b']) )

One approach:

一种方法:

df = pd.DataFrame({'a':['dfg','f','fff','fgrf','fghj'], 'b' : ['sd','dfg','edr','df','fghjky']})

df['c'] = df.apply(lambda x: max([len(x) for x in [df['a'], df['b']]]))
print df
      a       b   c
0   dfg      sd NaN
1     f     dfg NaN
2   fff     edr NaN
3  fgrf      df NaN
4  fghj  fghjky NaN

采纳答案by jezrael

You can use function mapand select by function np.wheremore info

您可以使用功能映射并按功能选择np.where更多信息

print df
#     a     b
#0  aaa  rrrr
#1   bb     k
#2  ccc     e
#condition if condition is True then len column a else column b
df['c'] = np.where(df['a'].map(len) > df['b'].map(len), df['a'].map(len), df['b'].map(len))
print df
#     a     b  c
#0  aaa  rrrr  4
#1   bb     k  2
#2  ccc     e  3

Next solution is with function applywith parameter axis=1:

下一个解决方案是使用带有参数的函数应用axis=1

axis = 1 or ‘columns': apply function to each row

axis = 1 或 'columns':对每一行应用函数

df['c'] = df.apply(lambda x: max(len(x['a']), len(x['b'])), axis=1)