按特定顺序重组数据框

时间:2017-08-03 19:45:10

标签: python python-3.x pandas dataframe

大家好,所以我有2个数据帧我正在尝试合并并按特定顺序分组:

  <?php
$servername = "xxx";
 $username = "xxx";
 $password = "xxx";


  $v1 = "coalsmil_wp282";
  $v2 = "coalsmil_wp111";
  $v3 = "coalsmil_wp72";
  $v4 = "coalsmil_wp193";
  $v5 = "coalsmil_wp555";
  $v6 = "coalsmil_wp366";
  $v7 = "coalsmil_wp74";
  $v8 = "coalsmil_wp721";
  $v9 = "coalsmil_wp924";
  $v10 = "coalsmil_wp253";


      // Create connection
    $conn = new mysqli($servername, $username, $password);

  // Check connection
      if ($conn->connect_error) {
          die("Connection failed: " . $conn->connect_error);
       }
      echo "Connected successfully";

   //Here I connect to both databases
    $query103 = mysqli_query($conn, "SELECT * FROM `$v1`.`wpd2_posts` 
        where post_status='wc-processing' or post_status='wc-completed'
           or post_status='wc-failed' UNION
     SELECT * FROM `$v2`.`wpd2_posts` 
       where post_status='wc-processing' or post_status='wc-completed'
        or post_status='wc-failed' order by post_date DESC ") or           die(mysqli_error($conn));



 ?>

 <br>
    <h2>Database 3</h2>

 <table style="width: 100%">
             <tr>
             <td>ID</td>
             <td>??</td>
             <td>??</td>
             <td>??</td>
             <td>??</td>
             <td>??</td>


             </tr>
    <?php
         while($row = mysqli_fetch_array($query103))           
         {


            // Here I try to find what database the customer is on
                $id2 = $row['ID'];

                if($row['ID'] == ''){

                    echo "intet id";
                }else {

                    $query = mysqli_query($conn, "SELECT * FROM  `$v1`.`wpd2_posts` where ID = '$id2' ORDER BY id") or die(mysqli_error($conn));    

                            if(mysqli_num_rows($query) == '') {

                                $query = mysqli_query($conn, "SELECT * FROM  `$v2`.`wpd2_posts` where ID = '$id2' ORDER BY id") or die(mysqli_error($conn));    

                            if(mysqli_num_rows($query) == '') {

                        }else{
                                $version = "coalsmil_wp111";

                            }

                        }else{
                                $version = "coalsmil_wp282";

                            }


                }

                 echo "<tr>";
                 echo "<td>". $row['ID']."</td>";
                 echo "<td>". $row['post_date']."</td>";
                 echo "<td>". $row['post_status']."</td>";

                            //And here I try to get the data out


                                      $querynavn = mysqli_query($conn, "SELECT meta_value FROM  `$version`.`wpd2_postmeta` where meta_key='_shipping_first_name' and post_id='$id2' ") or die(mysqli_error($conn));
                  while($row2 = mysqli_fetch_array($querynavn))        
                  {
              $fornavn = urldecode($row2['meta_value']);
                }
            echo "<td>". $fornavn."</td>";
                 echo "<td>".$version."</td>";
                 echo "<td>6</td>";
                 echo "</tr>";

         }


         ?>
 </table>

我正在寻找合并它们,使得一个LC_REF的所有M都被堆叠,然后所有的H,然后是所有的P,然后移动到第二个LC_REF。订单无关紧要但应保持一致。希望这是有道理的:

df1   
   LC_REF     Category      PRDGRP
0  17 1C      H         Ferrari,Lambo,Merc
1  17 1C      M         Doritos,Lays,Funyun
2  17 1C      P         Cats,Dogs,Rabbits
3  16 2C      H         Aston,Hyundai,Honda
4  16 2C      M         Cheeto, Vicks
5  16 2C      P         Rat,Pig,Flamingo
6  17 2C      M         See,Sea,Far


df2   
   LC_REF     Category      PRDGRP
0  17 1C         H         foo,bar
1  17 1C         M         foo,bar1
2  16 2C         H         foo,bar2
3  16 2C         M         foo,bar3
4  17 2C         H         foo,bar4
5  17 2C         M         foo,bar5
6  17 2C         P         foo,bar6

我尝试过各种各样的concat,并且无济于事:

df3   
   LC_REF     Category      PRDGRP
0  17 1C       M         Doritos,Lays,Funyun
1  17 1C       M         foo,bar1
2  17 1C       H         Ferrari,Lambo,Merc
3  17 1C       H         foo,bar
4  17 1C       P         Cats,Dogs,Rabbits
5  16 2C       M         Cheeto, Vicks
6  16 2C       M         foo,bar3
7  16 2C       H         Aston,Hyundai,Honda
8  16 2C       H         foo,bar4
9  17 2C       M         See,Sea,Far
10  17 2C      M         foo,bar5
11  17 2C      P         foo,bar6

几乎接近,但LC_REF无序

1 个答案:

答案 0 :(得分:2)

让我们使用pd.concatsort_values

df_out = pd.concat([df1,df2])
df_out['Category'] = df_out.Category.astype('category', categories=['M','H','P'], ordered=True)
df_out.sort_values(by=['LC_REF','Category'])

输出:

  LC_REF Category               PRDGRP
4  16 2C        M        Cheeto, Vicks
3  16 2C        M             foo,bar3
3  16 2C        H  Aston,Hyundai,Honda
2  16 2C        H             foo,bar2
5  16 2C        P     Rat,Pig,Flamingo
1  17 1C        M  Doritos,Lays,Funyun
1  17 1C        M             foo,bar1
0  17 1C        H   Ferrari,Lambo,Merc
0  17 1C        H              foo,bar
2  17 1C        P    Cats,Dogs,Rabbits
6  17 2C        M          See,Sea,Far
5  17 2C        M             foo,bar5
4  17 2C        H             foo,bar4
6  17 2C        P             foo,bar6