在DataFrame Python 2.7.0.17.1中重复将列作为行添加

在DataFrame Python 2.7.0.17.1中重复将列作为行添加,python,pandas,Python,Pandas,我想合并两个数据帧,但它们不共享索引 第一个具有以下结构: 日期邮件\u卷 2011-01-01 100 2011-02-01 150 2011-03-01 125 … 第二个是使用分位数函数创建的: df.分位数([.25,50,75]) 其结构如下: mail\u卷 零点二五一一零 零点五零一二零 0.75 130 我想用以下结构创建第三个数据帧,其中分位数结果与原始结果一起每月重复: 日期邮件\u数量度量\u类型 2011-01-01 100结果 2011-01-01

我想合并两个数据帧,但它们不共享索引

第一个具有以下结构:

日期邮件\u卷
2011-01-01    100
2011-02-01    150
2011-03-01    125
…

第二个是使用分位数函数创建的:

df.分位数([.25,50,75])

其结构如下:

mail\u卷
零点二五一一零
零点五零一二零
0.75 130

我想用以下结构创建第三个数据帧,其中分位数结果与原始结果一起每月重复:

日期邮件\u数量度量\u类型
2011-01-01 100结果
2011-01-01    110            .25
2011-01-01    120            .50
2011-01-01    130            .75
2011-02-01 150结果
2011-02-01    110            .25
2011-02-01    120            .50
2011-02-01 130.75

我已经搜索了在数据框中添加行和插入行,但是这些并不能解决我的问题,因为需要重复日期并添加metric_类型列

提前感谢,,
Eric

让我们使用原始数据帧进行笛卡尔合并和concat:

pd.concat([df.assign(metric_type='result'),
           df.assign(key=1).merge(df2.reset_index().assign(key=1), on='key', suffixes=('_x',''))[['date','mail_volume','index']].rename(columns={'index':'metric_type'})])\
  .sort_values(by='date')
输出:

       date  mail_volume metric_type
0  2011-01-01          100      result
0  2011-01-01          110        0.25
1  2011-01-01          120         0.5
2  2011-01-01          130        0.75
1  2011-02-01          150      result
3  2011-02-01          110        0.25
4  2011-02-01          120         0.5
5  2011-02-01          130        0.75
2  2011-03-01          125      result
6  2011-03-01          110        0.25
7  2011-03-01          120         0.5
8  2011-03-01          130        0.75

让我们使用原始数据帧执行笛卡尔合并和concat:

pd.concat([df.assign(metric_type='result'),
           df.assign(key=1).merge(df2.reset_index().assign(key=1), on='key', suffixes=('_x',''))[['date','mail_volume','index']].rename(columns={'index':'metric_type'})])\
  .sort_values(by='date')
输出:

       date  mail_volume metric_type
0  2011-01-01          100      result
0  2011-01-01          110        0.25
1  2011-01-01          120         0.5
2  2011-01-01          130        0.75
1  2011-02-01          150      result
3  2011-02-01          110        0.25
4  2011-02-01          120         0.5
5  2011-02-01          130        0.75
2  2011-03-01          125      result
6  2011-03-01          110        0.25
7  2011-03-01          120         0.5
8  2011-03-01          130        0.75

我们可以使用mail\u volume连接两个数据帧吗我们可以使用mail\u volume连接两个数据帧吗