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
Creating a new column in Panda by using lambda function on two existing 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, df
has two columns a
and b
. I want to create a new column c
which is equal to the longest length between a
and b
.
例如,df
有两列a
和b
。我想创建一个新列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.where
more info
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)