如何逐个组织数据并将缺失值设置为零?

时间:2017-07-11 03:35:20

标签: python pandas

我每天玩几次游戏,每次都得分。我想逐小时重新组织数据,并将缺失值设置为零。

以下是原始数据:

$stmt = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
        . "LEFT JOIN employementdetails ed ON apd.ApplicantID = ed.ApplicantID "
        . "LEFT JOIN sourceoffunds sof ON apd.ApplicantID = sof.ApplicantID "
        . "LEFT JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
        . "WHERE apd.AccountID ='{$accountId}' AND applicantType ='main';");        

$stmt->execute();

if ($stmt->rowCount() > 0) {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);

        echo $row['EmploymentStatus'];
        ?>
        <?php
    }
} else {
    ?>
    <div class="">
        <div class="alert alert-warning">
            <span class="glyphicon glyphicon-info-sign"></span> &nbsp; No Data Found ...
        </div>
    </div>
    <?php
} 
?>

看起来像这样:

import pandas as pd

df = pd.DataFrame({
    'Time': ['2017-01-01 08:45:00', '2017-01-01 09:11:00', 
             '2017-01-01 11:40:00', '2017-01-01 14:05:00', 
             '2017-01-01 21:00:00'],
    'Score': range(1, 6)})

如何获得这样的新数据框:

   Score        Time
0   1   2017-01-01 08:45:00
1   2   2017-01-01 09:11:00
2   3   2017-01-01 11:40:00
3   4   2017-01-01 14:05:00
4   5   2017-01-01 15:00:00

非常感谢!

1 个答案:

答案 0 :(得分:2)

您可以the documentation使用某些汇总功能,例如sum,然后使用resample并转换为int fillna,但首先添加first }和last DateTime值:

df.loc[-1, 'Time'] = '2017-01-01 00:00:00'
df.loc[-2, 'Time'] = '2017-01-01 23:00:00'
df['Time'] =  pd.to_datetime(df['Time'])

df = df.resample('H', on='Time').sum().fillna(0).astype(int)
print (df)
                     Score
Time                      
2017-01-01 00:00:00      0
2017-01-01 01:00:00      0
2017-01-01 02:00:00      0
2017-01-01 03:00:00      0
2017-01-01 04:00:00      0
2017-01-01 05:00:00      0
2017-01-01 06:00:00      0
2017-01-01 07:00:00      0
2017-01-01 08:00:00      1
2017-01-01 09:00:00      2
2017-01-01 10:00:00      0
2017-01-01 11:00:00      3
2017-01-01 12:00:00      0
2017-01-01 13:00:00      0
2017-01-01 14:00:00      4
2017-01-01 15:00:00      0
2017-01-01 16:00:00      0
2017-01-01 17:00:00      0
2017-01-01 18:00:00      0
2017-01-01 19:00:00      0
2017-01-01 20:00:00      0
2017-01-01 21:00:00      5
2017-01-01 22:00:00      0
2017-01-01 23:00:00      0