python - 生成日期之间缺失的连续日期

标签 python pandas

我有一个动态生成的文件(即文件头保持不变但值发生变化)。例如,让文件具有这种形式:

ID,CLASS,DATE,MRK
1,321,02/12/2016,30
2,321,05/12/2016,40
3,321,06/12/2016,0
4,321,07/12/2016,60
5,321,10/12/2016,70
6,876,5/12/2016,100
7,876,7/12/2016,80

CLASS 321 的通知有一些缺失的日期,即 03/12/201604/12/201608/12/201609/12/2016。我正在尝试将缺失的日期插入到适当的位置,并且 MRK 的对应值为 0。预期的输出是这样的:

ID,CLASS,DATE,MRK
1,321,02/12/2016,30
2,321,03/12/2016,0
3,321,04/12/2016,0
4,321,05/12/2016,40
5,321,06/12/2016,0
6,321,07/12/2016,60
7,321,08/12/2016,0
8,321,09/12/2016,0
9,321,10/12/2016,70
10,876,5/12/2016,100
11,876,6/12/2016,0
12,876,7/12/2016,80

这是我到目前为止想出的:

import pandas as pd

df = pd.read_csv('In.txt')
resampled_df = df.resample('D').mean()
print resampled_df

但是我遇到了异常:

TypeError:仅对 DatetimeIndex、TimedeltaIndex 或 PeriodIndex 有效,但得到了“RangeIndex”的实例

有人可以在这里帮助 python 新手吗?

最佳答案

像这样阅读您的 CSV -

df = pd.read_csv('file.csv', 
                 sep=',', 
                 parse_dates=['DATE'],  
                 dayfirst=True,         # this is important since you have days first
                 index_col=['DATE'])

现在,调用groupby + resample + first,收尾 -

df = df.groupby('CLASS').resample('1D')[['MRK']].first() 

df.ID = np.arange(1, len(df) + 1)
df.MRK = df.MRK.fillna(0).astype(int)

df.reset_index()

    CLASS       DATE  ID  MRK
0     321 2016-12-02   1   30
1     321 2016-12-03   2    0
2     321 2016-12-04   3    0
3     321 2016-12-05   4   40
4     321 2016-12-06   5    0
5     321 2016-12-07   6   60
6     321 2016-12-08   7    0
7     321 2016-12-09   8    0
8     321 2016-12-10   9   70
9     876 2016-12-05  10  100
10    876 2016-12-06  11    0
11    876 2016-12-07  12   80

特别是,MRK 需要fillna。其余的可以向前填充。


如果列的顺序很重要,这是另一个版本。

df = pd.read_csv('file.csv', 
                 sep=',', 
                 parse_dates=['DATE'],  
                 dayfirst=True)

c = df.columns
df = df.set_index('DATE').groupby('CLASS').resample('1D')[['MRK']].first()

df['MRK'] = df.MRK.fillna(0).astype(int)
df['ID'] = np.arange(1, len(df) + 1)
df = df.reset_index().reindex(columns=c)
df['DATE'] = df['DATE'].dt.strftime('%d/%m/%Y')

df

    ID  CLASS        DATE  MRK
0    1    321  02/12/2016   30
1    2    321  03/12/2016    0
2    3    321  04/12/2016    0
3    4    321  05/12/2016   40
4    5    321  06/12/2016    0
5    6    321  07/12/2016   60
6    7    321  08/12/2016    0
7    8    321  09/12/2016    0
8    9    321  10/12/2016   70
9   10    876  05/12/2016  100
10  11    876  06/12/2016    0
11  12    876  07/12/2016   80

关于python - 生成日期之间缺失的连续日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47829950/

相关文章:

python - Scrapy MySQL : MySQLdb. _exceptions.ProgrammingError: 并非所有参数都在字节格式化期间转换

python - 从 groupby pandas 中获取 MultiIndex

excel - 基于Excel工作表中的单元格颜色和文本颜色子集数据框

python - 更改绘图的标记方向和图例位置

python - Python 统计事件数量

python - 如何将基本脚本中的代码转换为带有实例的类?

python - 为 3D 体积绘制 seaborn 热图动画时出错

python - PyQt4 : How can i toggle the "Stay On Top" behavior?

python - 在 python 中显示原始图像像素而不是掩码

python - Matplotlib 动画多个大圆圈并从 pandas df 更新