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

99封情书 提交于 2020-01-01 01:26:51

问题


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?

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.

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

回答1:


You can use function map and 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 apply with parameter axis=1:

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

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


来源:https://stackoverflow.com/questions/33680666/creating-a-new-column-in-panda-by-using-lambda-function-on-two-existing-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!