根据另一列的值向DataFrame添加具有特定列差异的列

时间:2019-10-24 04:54:52

标签: python pandas dataframe

我有一个看起来像这样的数据框:


+------------+------------------+--------+-----+-----+---+--------+-----------------------------+
|   B_date   |      B_Time      | F_Type | Fix | Est | S | C_Type |           C_Time            |
+------------+------------------+--------+-----+-----+---+--------+-----------------------------+
| 2019-07-22 | 16:42:27.7325458 |      1 | 100 | 100 | 2 |      2 | 2019-07-22 16:42:47.2129273 |
| 2019-07-22 | 16:44:04.7817750 |      1 | 100 | 100 | 2 |      2 | 2019-07-22 16:45:26.2923547 |
| 2019-07-22 | 16:48:21.5976290 |      1 | 100 | 100 | 7 |        |                             |
| 2019-07-23 | 13:11:20.4519581 |      1 | 100 | 100 | 7 |        |                             |
| 2019-07-23 | 13:28:49.5092331 |      1 | 100 | 100 | 2 |      2 | 2019-07-23 13:28:54.5274793 |
| 2019-07-23 | 13:29:06.6108796 |      1 | 100 | 100 | 2 |      2 | 2019-07-23 13:30:48.5358081 |
| 2019-07-23 | 13:31:12.7684213 |      1 | 100 | 100 | 2 |      3 | 2019-07-23 13:33:50.9405643 |
| 2019-07-25 | 09:32:12.7799801 |      1 | 105 | 105 | 7 |        |                             |
| 2019-07-25 | 09:57:58.4536238 |      1 | 158 | 158 | 4 |        |                             |
| 2019-07-25 | 10:03:22.7888221 |      1 | 152 | 152 | 2 |      2 | 2019-07-25 10:03:27.9576175 |
+------------+------------------+--------+-----+-----+---+--------+-----------------------------+

我需要获得如下输出:


+------------+-------------------------------+--------+-----+-----+---+--------+-------------------------------+---------------+-----------------+---------------+
|   B_date   |            B_Time             | F_Type | Fix | Est | S | C_Type |            C_Time             | cancel_diff_1 |  cancel_diff_2  | cancel_diff_3 |
+------------+-------------------------------+--------+-----+-----+---+--------+-------------------------------+---------------+-----------------+---------------+
| 2019-07-22 | 2019-07-22 16:42:27.732545800 |      1 | 100 | 100 | 2 | 2      | 2019-07-22 16:42:47.212927300 | NaT           | 00:00:19.480381 | NaT           |
| 2019-07-22 | 2019-07-22 16:44:04.781775000 |      1 | 100 | 100 | 2 | 2      | 2019-07-22 16:45:26.292354700 | NaT           | 00:01:21.510579 | NaT           |
| 2019-07-22 | 2019-07-22 16:48:21.597629000 |      1 | 100 | 100 | 7 | NaN    | NaT                           | NaT           | NaT             | NaT           |
| 2019-07-23 | 2019-07-23 13:11:20.451958100 |      1 | 100 | 100 | 7 | NaN    | NaT                           | NaT           | NaT             | NaT           |
| 2019-07-23 | 2019-07-23 13:28:49.509233100 |      1 | 100 | 100 | 2 | 2      | 2019-07-23 13:28:54.527479300 | NaT           | 00:00:05.018246 | NaT           |
+------------+-------------------------------+--------+-----+-----+---+--------+-------------------------------+---------------+-----------------+---------------+

我实际上已经使用一个函数来完成它,但是它并赋值并检查值(可以说是python方式),我想在简单的熊猫中做到这一点。

1 个答案:

答案 0 :(得分:1)

IIUC试试这个:

<?php 
$sql = "SELECT * from  tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
echo "<select name='user' multiple>";
foreach($results as $result)
{
?>  
<option value="<?php echo htmlentities($result->id);?>"><?php echo htmlentities($result->name);?></option>
<?php
}
echo "</select>";
}
?>