追加两个不等列的数据框

时间:2018-07-02 15:18:52

标签: python pandas

我正在尝试在熊猫中追加两个具有不同列数的数据框。

<?php 

    if(isset($_GET["email"]) && isset($_GET["token"])) {

    $connection = new mysqli("localhost", "USER", "PASSWORD", "USERDB");

    $email = $connection->real_escape_string($_GET["email"]);
    $token = $connection->real_escape_string($_GET["token"]);

    $data = $connection->query("SELECT user_id FROM users WHERE user_email='$email' AND user_token='$token'");

        if ($data->num_rows > 0) {

            echo '<html>

<head>

  <meta charset="UTF-8">

  <title>Change Password</title>

    <link rel="stylesheet" href="../css/style.css" media="screen" type="text/css" />

</head>

<body>

  <div class="reset">
    <h1>Password reset</h1>
            <form action="anotherpage.php" method="POST">
     <input type="password" name="pwd" placeholder="Password">
     <input type="hidden" name="token" value="$token">
     <input type="submit" name="submit" class="submit" value="Update">

</form>

</body>
</html>';

        }   else {


            echo "Please check your link!";
        }





    }   else {

        header("Location: ../");
        exit();
    }

 ?>

我正在使用 Example: df1 A B 1 1 2 2 3 3 df2 A 4 5 Expected concatenated dataframe df A B 1 1 2 2 3 3 4 Null(or)0 5 Null(or)0 ,当列相同时。但是不知道如何处理不相等的列数。

2 个答案:

答案 0 :(得分:2)

pd.concat怎么样?

>>> pd.concat([df1,df2])
   A    B
0  1  1.0
1  2  2.0
2  3  3.0
0  4  NaN
1  5  NaN

此外,df1.append(df2)仍然有效:

>>> df1.append(df2)
   A    B
0  1  1.0
1  2  2.0
2  3  3.0
0  4  NaN
1  5  NaN

来自df.append的{​​{3}}:

  

不在此框架中的列被添加为新列。

答案 1 :(得分:1)

使用concat连接两列并传递附加参数ignore_index=True来重置索引,否则您可能会以indexes作为0 1 2 0 1结尾。有关更多信息,请参见docs here

df1 = pd.DataFrame({'A':[1,2,3], 'B':[1,2,3]})
df2 = pd.DataFrame({'A':[4,5]})
df  = pd.concat([df1,df2],ignore_index=True)
df

输出:

没有ignore_index = True

    A   B
0   1   1.0
1   2   2.0
2   3   3.0
0   4   NaN
1   5   NaN

带有ignore_index = True

    A   B
0   1   1.0
1   2   2.0
2   3   3.0
3   4   NaN
4   5   NaN