Pandas:添加参数以应用多个输入

时间:2018-04-16 17:38:41

标签: python pandas

我想使用apply with two columns并添加其他参数。我的用例是对列执行搜索并将正则表达式返回到另一列而不覆盖另一列中的现有值。也许iterrows是一个更好的选择:)。

import random
import re
import pandas as pd
import numpy as np

    #create the dataframe
df = pd.DataFrame({ 

    'a':np.random.choice( ['the_panda','it_python','my_shark'], 6),        
    })
df["b"] = ""

收率:

    a   b
0   the_panda   
1   my_shark    
2   my_shark    
3   the_panda   
4   it_python   
5   the_panda   

如果值出现在“a”列中,每次我应用我的函数,那么我想将搜索字符串写入列“b”。因此,如果我使用“熊猫”然后“鲨鱼”进行搜索,它将如下所示:

a   b
0   the_panda   panda
1   my_shark    shark
2   my_shark    shark
3   the_panda   panda
4   it_python   
5   the_panda   panda

我创建了一个简单的函数:

def search_log(b,a,search_sting):
    so = re.search(search_string,a)
    if so:
        return search_string
    else:
        return b

但是我不确定在这种情况下是否有办法在apply函数中添加其他参数?这是我正在尝试的:

search_string = 'panda'
df['b'] = df.apply(lambda x: search_log(x['b'],x['a']),args=(search_string,),axis=1)

哪个收益率:

TypeError: ('<lambda>() takes 1 positional argument but 2 were given', 'occurred at index 0')

...或

df['b'] = df.apply(lambda x: search_log(x['b'],x['a'],args=(search_string,),axis=1))

产生:

KeyError: ('b', 'occurred at index a')

1 个答案:

答案 0 :(得分:1)

error[E0283]: type annotations required: cannot resolve `_: std::convert::Into<std::borrow::Cow<'_, str>>`
  --> src/main.rs:12:5
   |
12 |     foo(Some("aaa"));
   |     ^^^
   |
note: required by `foo`
  --> src/main.rs:3:1
   |
3  | / fn foo<'a, T, O>(_bar: O)
4  | | where
5  | |     T: Into<Cow<'a, str>>,
6  | |     O: Into<Option<T>>,
7  | | {}
   | |__^

输出:

string = ["panda","shark","python"]
df["b"] = df["a"].apply(lambda y:[x for x in string if x in y][0] if len([x for x in string if x in y])==1 else "")