根据子字符串列表过滤熊猫数据框

时间:2019-09-04 09:42:13

标签: python pandas

我有一个包含多个字符串列的Pandas Dataframe。 现在,我想根据允许的子字符串列表检查某一列,然后获得一个包含结果的新子集。

public function send_email()
    {   
        $getStaff=DB::table('staff')
        ->OrderBy('staff_id','DESC')
        ->first();
        $staffacckey=$getStaff->access_key;
        global $staffemail;
        $staffemail=$getStaff->email;
        $valueOne = env('APP_URL');
        $info = array();
        $info['url']=$valueOne.':8080';
        $info['name']='tinox';
        $info['msg']='test';
        $info['key']=$staffacckey;
        $info['email']=$staffemail;
        Mail::send('email.mail',$info,
        function($email)use ($info){
            $ghost='no-reply@magazinelte.com';
            $admin='System Administrator';
            $email->from($ghost, $admin);
            $email->to($info['email'])->subject('Account activation');
        });

    }

just fixed the issue!! Passed variables inside the Mail function and change my env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=*******@gmail.com
MAIL_PASSWORD=*******
MAIL_ENCRYPTION=tls

我发现的唯一方法是创建相应列的列表,然后进行列表理解,但是随后我松开了其他列。我可以将列表理解功能用作例如substr = ['A', 'C', 'D'] df = pd.read_excel('output.xlsx') df = df.dropna() # now filter all rows where the string in the 2nd column doesn't contain one of the substrings

df.str.contains()

预期输出:

year  type     value   price
2000  ty-A     500     10000
2002  ty-Q     200     84600
2003  ty-R     500     56000
2003  ty-B     500     18000
2006  ty-C     500     12500
2012  ty-A     500     65000
2018  ty-F     500     86000
2019  ty-D     500     51900

1 个答案:

答案 0 :(得分:2)

您可以使用pandas.Series.isin

>>> df.loc[df['type'].isin(substr)]
   year type  value  price
0  2000    A    500  10000
4  2006    C    500  12500
5  2012    A    500  65000
7  2019    D    500  51900