如何检查数据框列中是否存在项目列表

时间:2019-04-10 04:48:16

标签: python excel pandas

我有一个从excel文件中读取的字符串(句子)的数据帧列,并且我有一个字符串(关键字)列表

df['Purpose'] = ['Central Team Offiste Material - R2 Strengths Profiler test x 7', 'Project Green conference', 'had to book flight as late for flight due to transportation', 'Dublin Transition', 'Training - Dublin transition', 'HRLT Offsite in Dublin - seat choice', 'Baggage fare plus upgrade in flight class', 'Due to a family emergency Jeremy needed to fly home earlier', 'flight back to london after various clients meeting', 'Travel to UK']

Rule2_list=['Dublin', 'stakeholders', 'Travel', 'interviews', 'workshop', 'due-diligence', 'business trip', 'client', 'risk']

我想检查df ['Purpose']中是否存在Rule2_list中的任何列表元素

我该如何实现?任何帮助都非常感激

1 个答案:

答案 0 :(得分:0)

您可以使用https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.contains.html#pandas.Series.str.contains

session_start();
    include "required/connection.php";



    $str_json = file_get_contents('php://input');
    //Decoding received JSON to array
    $response = json_decode($str_json,true); 
    //Get values

    $hotel_city = $response['hotel_city'];
    $Room_Type_Name = $response['Room_Type_Name'];  
    $count_of_travels = $response['count_of_travels'];  
    $count_of_childerns= $response['count_of_childerns'];
    $price= $response['price']; 
    $split_price=explode("-",$price);
    $min = $split_price[0];
   $max = $split_price[1];
   $checkin =$response['checkin'];
   $checkout =$response['checkout'];

    //Concatenate sql string considering users inputs
    //$sql = "SELECT * FROM room";
    if($hotel_city!=="" || $count_of_travels !=="" || $Room_Type_Name!=="" ||$count_of_childerns!==""||$min!==""||$max!=="" ||$checkin!=="" || $checkout!=="" ) {
        $sql = "SELECT *.reservation, room.room_id,room.short_description,room.count_of_travels,room.count_of_childerns,room.price,hotels.hotel_id,hotels.hotel_name,hotels.hotel_city,hotels.hotel_picture,room_type.Room_Type_Id,room_type.Room_Type_Name FROM hotels INNER JOIN room ON room.Hotel_Id=hotels.hotel_id INNER JOIN room_type on room.room_type=room_type.Room_Type_Id INNER JOIN on  reservation  Where";

    }
     if($hotel_city!=="" && $count_of_travels !=="" && $Room_Type_Name!=="" && $count_of_childerns!=="") {
         if($min=="" && $max=="")
         {
             $sql =  $sql.  " hotel_city='$hotel_city' AND count_of_travels='$count_of_travels' AND Room_Type_Name='$Room_Type_Name' && count_of_childerns='$count_of_childerns' ORDER by hotels.hotel_name,room_type.Room_Type_Name";

         }
         else
         {
             if($min!=="" && $max=="")
             {
                  $split_min=explode("+",$min);
                  $sql =  $sql.  " hotel_city='$hotel_city' AND count_of_travels='$count_of_travels' AND Room_Type_Name='$Room_Type_Name' && count_of_childerns='$count_of_childerns' AND price >= '$split_min[0]' ORDER by hotels.hotel_name,room_type.Room_Type_Name";
             }
             else
             {
                $sql =  $sql.  " hotel_city='$hotel_city' AND count_of_travels='$count_of_travels' AND Room_Type_Name='$Room_Type_Name' && count_of_childerns='$count_of_childerns' AND price BETWEEN '$min' AND '$max' ORDER by hotels.hotel_name,room_type.Room_Type_Name"; 
             }
         }

    }
    else if($hotel_city!=="" && $count_of_travels !=="" && $Room_Type_Name!=="" ) {
         if($min=="" && $max=="")
         {
             $sql =  $sql.  " hotel_city='$hotel_city' AND count_of_travels='$count_of_travels' AND Room_Type_Name='$Room_Type_Name'  ORDER by hotels.hotel_name,room_type.Room_Type_Name";

         }
         else
         {
             if($min!=="" && $max=="")
             {
                  $split_min=explode("+",$min);
                  $sql =  $sql.  " hotel_city='$hotel_city' AND count_of_travels='$count_of_travels' AND Room_Type_Name='$Room_Type_Name'  AND price >= '$split_min[0]' ORDER by hotels.hotel_name,room_type.Room_Type_Name";
             }
             else
             {
                $sql =  $sql.  " hotel_city='$hotel_city' AND count_of_travels='$count_of_travels' AND Room_Type_Name='$Room_Type_Name'  AND price BETWEEN '$min' AND '$max' ORDER by hotels.hotel_name,room_type.Room_Type_Name"; 
             }
         }

    }
     else if($hotel_city!=="" && $count_of_travels !==""  ) {
         if($min=="" && $max=="")
         {
             $sql =  $sql.  " hotel_city='$hotel_city' AND count_of_travels='$count_of_travels'   ORDER by hotels.hotel_name,room_type.Room_Type_Name";

         }
         else
         {
             if($min!=="" && $max=="")
             {
                  $split_min=explode("+",$min);
                  $sql =  $sql.  " hotel_city='$hotel_city' AND count_of_travels='$count_of_travels'  AND price >= '$split_min[0]' ORDER by hotels.hotel_name,room_type.Room_Type_Name";
             }
             else
             {
                $sql =  $sql.  " hotel_city='$hotel_city' AND count_of_travels='$count_of_travels'   AND price BETWEEN '$min' AND '$max' ORDER by hotels.hotel_name,room_type.Room_Type_Name"; 
             }
         }

    }
     else if($hotel_city!=="" ) {


         if($min=="" && $max=="")
         {
             $sql =  $sql.  " hotel_city='$hotel_city'    ORDER by hotels.hotel_name,room_type.Room_Type_Name";

         }
         else
         {
             if($min!=="" && $max=="")
             {
                  $split_min=explode("+",$min);
                  $sql =  $sql.  " hotel_city='$hotel_city'  AND price >= '$split_min[0]' ORDER by hotels.hotel_name,room_type.Room_Type_Name";
             }
             else
             {
                $sql =  $sql.  " hotel_city='$hotel_city'    AND price BETWEEN '$min' AND '$max' ORDER by hotels.hotel_name,room_type.Room_Type_Name"; 
             }
         }

    }
         else if($count_of_travels!=="" ) {


         if($min=="" && $max=="")
         {
             $sql =  $sql.  " count_of_travels='$count_of_travels'    ORDER by hotels.hotel_name,room_type.Room_Type_Name";

         }
         else
         {
             if($min!=="" && $max=="")
             {
                  $split_min=explode("+",$min);
                  $sql =  $sql.  " count_of_travels='$count_of_travels'  AND price >= '$split_min[0]' ORDER by hotels.hotel_name,room_type.Room_Type_Name";
             }
             else
             {
                $sql =  $sql.  " count_of_travels='$count_of_travels'    AND price BETWEEN '$min' AND '$max' ORDER by hotels.hotel_name,room_type.Room_Type_Name"; 
             }
         }

    }
    else if($Room_Type_Name!=="" ) {
         if($min=="" && $max=="")
         {
             $sql =  $sql.  " Room_Type_Name='$Room_Type_Name'    ORDER by hotels.hotel_name,room_type.Room_Type_Name";
         }
         else
         {
             if($min!=="" && $max=="")
             {
                  $split_min=explode("+",$min);
                  $sql =  $sql.  " Room_Type_Name='$Room_Type_Name'  AND price >= '$split_min[0]' ORDER by hotels.hotel_name,room_type.Room_Type_Name";
             }
             else
             {
                $sql =  $sql.  " Room_Type_Name='$Room_Type_Name'    AND price BETWEEN '$min' AND '$max' ORDER by hotels.hotel_name,room_type.Room_Type_Name"; 
             }
         }

    }
     else if($count_of_childerns!=="" ) {
         if($min=="" && $max=="")
         {
             $sql =  $sql.  " count_of_childerns='$count_of_childerns'    ORDER by hotels.hotel_name,room_type.Room_Type_Name";
         }
         else
         {
             if($min!=="" && $max=="")
             {
                  $split_min=explode("+",$min);
                  $sql =  $sql.  " count_of_childerns='$count_of_childerns'  AND price >= '$split_min[0]' ORDER by hotels.hotel_name,room_type.Room_Type_Name";
             }
             else
             {
                $sql =  $sql.  " count_of_childerns='$count_of_childerns'    AND price BETWEEN '$min' AND '$max' ORDER by hotels.hotel_name,room_type.Room_Type_Name"; 
             }
         }

    }
     else if($min!=="" &&  $max!=="" ) {
             if($min!=="" && $max=="")
             {
                  $split_min=explode("+",$min);
                  $sql =  $sql.  " price >= '$split_min[0]' ORDER by hotels.hotel_name,room_type.Room_Type_Name";
             }
             else
             {
                $sql =  $sql.  " price BETWEEN '$min' AND '$max' ORDER by hotels.hotel_name,room_type.Room_Type_Name"; 
             }
    }
    else
    {
         $sql = "SELECT room.room_id,room.short_description,room.count_of_travels,room.count_of_childerns,room.price,hotels.hotel_id,hotels.hotel_name,hotels.hotel_city,hotels.hotel_picture,room_type.Room_Type_Id,room_type.Room_Type_Name FROM hotels INNER JOIN room ON room.Hotel_Id=hotels.hotel_id INNER JOIN room_type on room.room_type=room_type.Room_Type_Id";

    }
  //  echo $sql;
  //exit;

        $result = $connection->query($sql);

    if ($result->num_rows > 0) {

        // output data of each row
        while($row = $result->fetch_assoc()) {
           ?>





   <article class="one-third">

<figure><a href="hotel.php?id=<?php echo $row["room_id"]; echo ';' ; echo $row["hotel_id"]; echo";";  echo $row["Room_Type_Id"]; ?>" title=""><img src="admin/images/Hotels/<?php echo $row['hotel_picture']; ?>" alt="<?php echo $row['hotel_name']; ?>" /></a></figure>
<div class="details">
<h3><?php echo $row['hotel_name']; ?>
<span class="stars">
<i class="material-icons">&#xE838;</i>
<i class="material-icons">&#xE838;</i>
<i class="material-icons">&#xE838;</i>
<i class="material-icons">&#xE838;</i>
<i class="material-icons">&#xE838;</i>
</span>
 </h3>
<span class="address">Type•<?php echo $row['Room_Type_Name'] ?></span>
<span class="address">Adults Allowed:<?php echo $row['count_of_travels'] ?></span>
<span class="address">Children Allowed:<?php echo $row['count_of_childerns'] ?></span>
 <span class="address">City: <?php echo $row['hotel_city'] ?>  •  <a href="map.php">Show on map</a></span>
<span class="rating"> 8 /10</span>
<span class="price">Price per room per night from  <em>Rs:<?php echo $row['price']; ?></em> </span>
<div class="description">
 <p><?php echo $row['short_description']; ?> </p>
</div>

<a href="hotel.php?id=<?php echo $row["room_id"]; echo ';' ; echo $row["hotel_id"]; echo";";  echo $row["Room_Type_Id"]; ?>" class="gradient-button">Book Now</a>
</div>
</article>