【问题标题】:Fill missing timeseries data using pandas or numpy使用 pandas 或 numpy 填充缺失的时间序列数据
【发布时间】:2015-11-21 09:10:37
【问题描述】:

我有一个字典列表,如下所示:

L=[
{
"timeline": "2014-10", 
"total_prescriptions": 17
}, 
{
"timeline": "2014-11", 
"total_prescriptions": 14
}, 
{
"timeline": "2014-12", 
"total_prescriptions": 8
},
{
"timeline": "2015-1", 
"total_prescriptions": 4
}, 
{
"timeline": "2015-3", 
"total_prescriptions": 10
}, 
{
"timeline": "2015-4", 
"total_prescriptions": 3
} 
]

这基本上是 SQL 查询的结果,当给出开始日期和结束日期时,会给出从开始日期到结束月份的每个月的总处方计数。但是,对于处方计数为的月份0(2015 年 2 月),它完全跳过了那个月。是否可以使用 pandas 或 numpy 来更改此列表,以便为缺少的月份添加一个条目,其中 0 作为总处方如下:

[
{
"timeline": "2014-10", 
"total_prescriptions": 17
}, 
{
"timeline": "2014-11", 
"total_prescriptions": 14
}, 
{
"timeline": "2014-12", 
"total_prescriptions": 8
{
"timeline": "2015-1", 
"total_prescriptions": 4
}, 
{
"timeline": "2015-2",   # 2015-2 to be inserted for missing month
"total_prescriptions": 0 # 0 to be inserted for total prescription
}, 
{
"timeline": "2015-3", 
"total_prescriptions": 10
}, 
{
"timeline": "2015-4", 
"total_prescriptions": 3
} 
]

【问题讨论】:

    标签: python list numpy dictionary pandas


    【解决方案1】:

    您所说的在 Pandas 中称为“重采样”;首先将您的时间转换为 numpy 日期时间并设置为您的索引:

    df = pd.DataFrame(L)
    df.index=pd.to_datetime(df.timeline,format='%Y-%m')
    df
               timeline  total_prescriptions
    timeline                                
    2014-10-01  2014-10                   17
    2014-11-01  2014-11                   14
    2014-12-01  2014-12                    8
    2015-01-01   2015-1                    4
    2015-03-01   2015-3                   10
    2015-04-01   2015-4                    3
    

    然后您可以使用resample('MS')(MS 代表“月份开始”我猜)添加您缺少的月份,并使用fillna(0) 将空值转换为零,以满足您的要求。

    df = df.resample('MS').fillna(0)
    df
                total_prescriptions
    timeline                       
    2014-10-01                   17
    2014-11-01                   14
    2014-12-01                    8
    2015-01-01                    4
    2015-02-01                  NaN
    2015-03-01                   10
    2015-04-01                    3
    

    要转换回原始格式,请使用to_native_types 将日期时间索引转换回字符串,然后使用to_dict('records') 导出:

    df['timeline']=df.index.to_native_types()
    df.to_dict('records')
    [{'timeline': '2014-10-01', 'total_prescriptions': 17.0},
     {'timeline': '2014-11-01', 'total_prescriptions': 14.0},
     {'timeline': '2014-12-01', 'total_prescriptions': 8.0},
     {'timeline': '2015-01-01', 'total_prescriptions': 4.0},
     {'timeline': '2015-02-01', 'total_prescriptions': 0.0},
     {'timeline': '2015-03-01', 'total_prescriptions': 10.0},
     {'timeline': '2015-04-01', 'total_prescriptions': 3.0}]
    

    【讨论】:

    • 这真是太好了..正是我需要的..一旦添加了缺失的日期,您知道如何将 df 转换回字典列表...
    • 好的..我想通了..df.to_dict('records')..非常感谢您对此的帮助
    • 好的..我有点太兴奋了..当我这样做时..它只是给了我total_prescriptions..我将如何取回原始列表
    • 我添加了转换回原始格式的说明
    猜你喜欢
    • 2020-02-28
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2019-03-06
    • 2022-11-02
    • 1970-01-01
    • 2015-12-03
    相关资源
    最近更新 更多