python - 在 MultiIndex 中为缺失日期插入 0 值

标签 python pandas

假设我有一个 MultiIndex,它由日期和一些类别(在下面的示例中为简单起见,一个类别)组成,并且对于每个类别,我都有一个包含某些过程值的时间序列。 我只有在有观察时才有值(value),现在我想在那个日期没有观察时添加一个“0”。 我发现了一种看起来非常低效的方法(堆叠和拆堆叠,这将在数百万类别的情况下创建许多列)。

import datetime as dt
import pandas as pd

days= 4
#List of all dates that should be in the index
all_dates = [datetime.date(2013, 2, 13) - dt.timedelta(days=x)
    for x in range(days)]
df = pd.DataFrame([
    (datetime.date(2013, 2, 10), 1, 4),
    (datetime.date(2013, 2, 10), 2, 7),
    (datetime.date(2013, 2, 11), 2, 7),
    (datetime.date(2013, 2, 13), 1, 2),
    (datetime.date(2013, 2, 13), 2, 3)],
    columns = ['date', 'category', 'value'])
df.set_index(['date', 'category'], inplace=True)
print df
print df.unstack().reindex(all_dates).fillna(0).stack()
# insert 0 values for missing dates
print all_dates

                        value
date       category       
2013-02-10 1             4
           2             7
2013-02-11 2             7
2013-02-13 1             2
           2             3

                      value
            category       
2013-02-13 1             2
           2             3
2013-02-12 1             0
           2             0
2013-02-11 1             0
           2             7
2013-02-10 1             4
           2             7
[datetime.date(2013, 2, 13), datetime.date(2013, 2, 12),
    datetime.date(2013, 2, 11),     datetime.date(2013, 2, 10)]

有人知道实现相同目标的更聪明的方法吗?

编辑:我发现了另一种实现相同目标的可能性:

import datetime as dt
import pandas as pd

days= 4
#List of all dates that should be in the index
all_dates = [datetime.date(2013, 2, 13) - dt.timedelta(days=x) for x in range(days)]
df = pd.DataFrame([(datetime.date(2013, 2, 10), 1, 4, 5),
(datetime.date(2013, 2, 10), 2,1, 7),
(datetime.date(2013, 2, 10), 2,2, 7),
(datetime.date(2013, 2, 11), 2,3, 7),
(datetime.date(2013, 2, 13), 1,4, 2),
(datetime.date(2013, 2, 13), 2,4, 3)],
columns = ['date', 'category', 'cat2', 'value'])
date_col = 'date'
other_index = ['category', 'cat2']
index = [date_col] + other_index
df.set_index(index, inplace=True)
grouped = df.groupby(level=other_index)
df_list = []
for i, group in grouped:
    df_list.append(group.reset_index(level=other_index).reindex(all_dates).fillna(0))
print pd.concat(df_list).set_index(other_index, append=True)

                    value
           category cat2       
2013-02-13 1        4         2
2013-02-12 0        0         0
2013-02-11 0        0         0
2013-02-10 1        4         5
2013-02-13 0        0         0
2013-02-12 0        0         0
2013-02-11 0        0         0
2013-02-10 2        1         7
2013-02-13 0        0         0
2013-02-12 0        0         0
2013-02-11 0        0         0
2013-02-10 2        2         7
2013-02-13 0        0         0
2013-02-12 0        0         0
2013-02-11 2        3         7
2013-02-10 0        0         0
2013-02-13 2        4         3
2013-02-12 0        0         0
2013-02-11 0        0         0
2013-02-10 0        0         0

最佳答案

你可以根据你想要的索引级别的笛卡尔积来制作一个新的多重索引。然后,使用新索引重新索引您的数据框。

(date_index, category_index) = df.index.levels
new_index = pd.MultiIndex.from_product([all_dates, category_index])
new_df = df.reindex(new_index)

# Optional: convert missing values to zero, and convert the data back
# to integers. See explanation below.
new_df = new_df.fillna(0).astype(int)

就是这样!新数据框具有所有可能的索引值。现有数据已正确编入索引。

继续阅读以获得更详细的解释。


解释

设置示例数据

import datetime as dt
import pandas as pd

days= 4
#List of all dates that should be in the index
all_dates = [dt.date(2013, 2, 13) - dt.timedelta(days=x)
    for x in range(days)]
df = pd.DataFrame([
    (dt.date(2013, 2, 10), 1, 4),
    (dt.date(2013, 2, 10), 2, 7),
    (dt.date(2013, 2, 11), 2, 7),
    (dt.date(2013, 2, 13), 1, 2),
    (dt.date(2013, 2, 13), 2, 3)],
    columns = ['date', 'category', 'value'])
df.set_index(['date', 'category'], inplace=True)

这是示例数据的样子

                     value
date       category
2013-02-10 1             4
           2             7
2013-02-11 2             7
2013-02-13 1             2
           2             3

创建新索引

使用 from_product我们可以创建一个新的多索引。这个新索引是 Cartesian product您传递给函数的所有值。

(date_index, category_index) = df.index.levels

new_index = pd.MultiIndex.from_product([all_dates, category_index])

重建索引

使用新索引重新索引现有数据框。

现在所有可能的组合都出现了。缺失值为空 (NaN)。

new_df = df.reindex(new_index)

现在,扩展后的重新索引数据框如下所示:

              value
2013-02-13 1    2.0
           2    3.0
2013-02-12 1    NaN
           2    NaN
2013-02-11 1    NaN
           2    7.0
2013-02-10 1    4.0
           2    7.0

整数列中的空值

可以看到新数据框中的数据已经从整数转换为 float 。 Pandas can't have nulls in an integer column .或者,我们可以将所有空值转换为 0,并将数据转换回整数。

new_df = new_df.fillna(0).astype(int)

结果

              value
2013-02-13 1      2
           2      3
2013-02-12 1      0
           2      0
2013-02-11 1      0
           2      7
2013-02-10 1      4
           2      7

关于python - 在 MultiIndex 中为缺失日期插入 0 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14856941/

相关文章:

python - 如何查找并替换txt文件中的属性?

Python 日志记录配置文件模块和包

python - 通过 Python 与 Windows 控制台应用程序交互

python - 如何从 Pandas 数据框中每 7 条记录具有最大值的数据框中获取整行?

python - 为 Pandas 中的每个键创建一个值列表?

python - 从两个数据框中获取匹配字符串的索引

python - Pygame 已安装;然而,python 终端显示 "No module named ' pygame' "(Ubuntu 20.04.1)

python - 远程连接到一个简单的套接字python服务器

python - 具有相同索引的两个数据帧的 Pandas 外积

excel - 如何在 Pandas 数据框列中插入逗号作为千位分隔符?