Python - 多列上的Lambda函数

时间:2017-01-19 20:54:49

标签: python pandas lambda

我有一个pandas数据框,我更喜欢使用lambda函数而不是循环来解决我的问题。

问题是这样的;

df = pd.DataFrame({'my_fruits':['fruit', 'fruit', 'fruit', 'fruit', 'fruit'],
         'fruit_a': ['apple', 'banana', 'vegetable', 'vegetable', 'cherry'],
         'fruit_b': ['vegetable', 'apple', 'vegeatble', 'pineapple', 'pear']})

如果我应用以下循环;

for i in np.arange(0,len(df)):
    if df['fruit_a'][i] == 'vegetable' or df['fruit_b'][i] == 'vegetable':
        df['my_fruits'][i] = 'not_fruit'

我能够得到我想要的结果。这是因为如果fruit_afruit_b列包含值vegetable,我希望my_fruits列等于not_fruit

我怎样才能在lamda函数中设置它。无法理解两列输入​​如何用于更改不同的列值。谢谢!

3 个答案:

答案 0 :(得分:3)

您可以.live使用Series.mask

boolean mask

mask = (df['fruit_a'] == 'vegetable') | (df['fruit_b'] == 'vegetable') print (mask) 0 True 1 False 2 True 3 True 4 False dtype: bool df.my_fruits = df.my_fruits.mask(mask, 'not_fruits') print (df) fruit_a fruit_b my_fruits 0 apple vegetable not_fruits 1 banana apple fruit 2 vegetable vegetable not_fruits 3 vegetable pineapple not_fruits 4 cherry pear fruit 的另一个解决方案是按mask比较所有选定的列,然后any至少在一列中获取所有vegetable

True

答案 1 :(得分:2)

使用pd.Series.where并在一步中检查'vegetable'是否与any相结合 wheremask相反,这就是我使用cond的否定的原因 否则,这与jezrael的回答非常相似

cond = df[['fruit_a', 'fruit_b']].eq('vegetable').any(1)
df.my_fruits = df.my_fruits.where(~cond, 'not_fruit')

从我的手机回答。请原谅错别字。

答案 2 :(得分:1)

您可以使用apply方法执行此操作:

https://api.coursera.org/api/catalog.v1/courses?fields=smallIcon%2CshortDescription&q=search&query=python
Got Positive response!
#<HTTParty::Response:0x2660970 parsed_response={"elements"=>[{"id"=>119, "shortName"=>"scicomp", "name"=>"High Performance Scientific Computing", "shortDescription"=>"Programming-oriented course on effectively using modern computers to solve scientific computing problems arising in the physical/engineering sciences and other fields. Provides an introduction to efficient serial and parallel computing using Fortran 90, OpenMP, MPI, and Python, and software development tools such as version control, Makefiles, and debugging.", "smallIcon"=>"https://d15cw65ipctsrr.cloudfront.net/00/621b9b2597807229ed0fa605f96cdc/HighPerformanceComputingIma.jpg", "links"=>{}}, {"id"=>87, "shortName"=>"compphoto", "name"=>"Computational Photography", "shortDescription"=>"In this course you will learn about the basics of 
....

或者你可以这样做:

>>> df.my_fruits = df.apply(lambda x: 'not_fruit' if x['fruit_a'] == 'vegetable' or x['fruit_b'] == 'vegetable' else x['my_fruits'], axis=1)
0    not_fruit
1        fruit
2    not_fruit
3    not_fruit
4        fruit