python - 数据操作开始日期结束日期 python pandas

标签 python pandas

我有一个促销描述数据集,其中包含有关正在运行的各种促销及其开始日期和结束日期的信息:

promo        item      start_date      end_date 

Buy1-get 1     A        2015-01-08    2015-01-12

Buy1-get 1     A        2015-02-16    2015-02-20

Buy1-40% off   B        2016-05-08      2016-05-09

现在我想组织我的数据以供后续分析,这样我就只有带有促销信息的单个可变日期。

date            item       Promo

2015-01-08       A         Buy1-get 1 

2015-01-09       A         Buy1-get 1 
2015-01-10       A         ......
2015-01-11       ....
2015-01-12

2015-02-16       A         Buy1-get 1 
2015-02-17       A         Buy1-get 1 
2015-02-18       ....      .......
2015-02-19       .....

..........
2016-05-09       B         Buy1-40% off

非常感谢任何帮助。

最佳答案

您可以使用 concat date_range 创建的所有系列itertuples然后 join promoitem 列:

df1 = pd.concat([pd.Series(r.Index,
                           pd.date_range(r.start_date,r.end_date)) for r in df.itertuples()])
        .reset_index()
df1.columns = ['date','idx']
df1 = df1.set_index('idx')
df1 = df1.join(df[['item','promo']]).reset_index(drop=True)
print (df1)
         date item         promo
0  2015-01-08    A    Buy1-get 1
1  2015-01-09    A    Buy1-get 1
2  2015-01-10    A    Buy1-get 1
3  2015-01-11    A    Buy1-get 1
4  2015-01-12    A    Buy1-get 1
5  2015-02-16    A    Buy1-get 1
6  2015-02-17    A    Buy1-get 1
7  2015-02-18    A    Buy1-get 1
8  2015-02-19    A    Buy1-get 1
9  2015-02-20    A    Buy1-get 1
10 2016-05-08    B  Buy1-40% off
11 2016-05-09    B  Buy1-40% off

另一种解决方案 meltgroupby with resample :

df1 = df.reset_index().rename(columns={'index':'idx'})
df1 = pd.melt(df1, id_vars='idx', value_vars=['start_date','end_date'], value_name='date')
        .set_index('date')
df1 = df1.groupby('idx')
         .resample('d')
         .ffill()
         .reset_index(level=1)
         .drop(['idx','variable'], axis=1)
df1 = df1.join(df[['item','promo']]).reset_index(drop=True)
print (df1)
         date item         promo
0  2015-01-08    A    Buy1-get 1
1  2015-01-09    A    Buy1-get 1
2  2015-01-10    A    Buy1-get 1
3  2015-01-11    A    Buy1-get 1
4  2015-01-12    A    Buy1-get 1
5  2015-02-16    A    Buy1-get 1
6  2015-02-17    A    Buy1-get 1
7  2015-02-18    A    Buy1-get 1
8  2015-02-19    A    Buy1-get 1
9  2015-02-20    A    Buy1-get 1
10 2016-05-08    B  Buy1-40% off
11 2016-05-09    B  Buy1-40% off

关于python - 数据操作开始日期结束日期 python pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41542769/

相关文章:

python - 测量 Pandas 系列列中 NaN 间隙的持续时间

python - 值错误 : Could not interpret input 'index' when using index with seaborn lineplot

python - 如何在 mac 中安装 setuptools

python - 在Python中管道合并多个pandas数据帧

python - 如何迭代 pandas 数据框中的每一列和每个单元格

python - 无法在Python中写入具有完整路径的文件

python检查序列中的位是真还是假

python - 在 Windows 上打开一个目录 : 'Permission denied'

python - Pandas dataframe,每个单元格都进入列表 - 更pythonic的方式?

python - 在列组合上扩展数据框