我有两个数据框,这是其中之一,它包含去年的数据:
col1 col2 col3 col4
Type Date
type1 2020-01-01 1.0 2.0 3.0 4.0
2020-02-01 1.0 2.0 3.0 4.0
2020-03-01 1.0 2.0 3.0 4.0
2020-04-01 1.0 2.0 3.0 4.0
2020-05-01 1.0 2.0 3.0 4.0
type2 2020-01-01 1.0 2.0 3.0 4.0
2020-02-01 1.0 2.0 3.0 4.0
2020-03-01 1.0 2.0 3.0 4.0
2020-04-01 1.0 2.0 3.0 4.0
2020-05-01 1.0 2.0 3.0 4.0
type3 2020-01-01 1.0 2.0 3.0 4.0
2020-02-01 1.0 2.0 3.0 4.0
2020-03-01 1.0 2.0 3.0 4.0
2020-04-01 1.0 2.0 3.0 4.0
2020-05-01 1.0 2.0 3.0 4.0
type4 2020-01-01 1.0 2.0 3.0 4.0
2020-02-01 1.0 2.0 3.0 4.0
2020-03-01 1.0 2.0 3.0 4.0
2020-04-01 1.0 2.0 3.0 4.0
2020-05-01 1.0 2.0 3.0 4.0
type5 2020-01-01 1.0 2.0 3.0 4.0
2020-02-01 1.0 2.0 3.0 4.0
2020-03-01 1.0 2.0 3.0 4.0
2020-04-01 1.0 2.0 3.0 4.0
2020-05-01 1.0 2.0 3.0 4.0
另一个是新数据:
col1 col2 col3 col4
Type Date
type1 2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type2 2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type3 2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type4 2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type5 2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
如你所见,不同的只是日期和值。我每天都创建这个新数据,我想在某些情况下将这两个数据框连接起来。我想看看去年的数据,以便进行比较。例如,因为我们现在是2021年1月,所以我只想在这里添加2020年1月的数据。当日期为2月时,此数据框应不再包含2020年1月的数据,只包含2020年2月的数据。但是,我在这里添加了所需的输出:
When the date is January 2021:
col1 col2 col3 col4
Type Date
type1 2020-01-01 1.0 2.0 3.0 4.0
2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type2 2020-01-01 1.0 2.0 3.0 4.0
2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type3 2020-01-01 1.0 2.0 3.0 4.0
2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type4 2020-01-01 1.0 2.0 3.0 4.0
2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type5 2020-01-01 1.0 2.0 3.0 4.0
2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
怎么做呢?
想法是通过Timestamp.to_period
将实际时间转换为月周期,减去上一年的12,并通过Index.get_level_values
选择的过滤器二级Date
,通过DatetimeIndex.to_period
转换为周期:
now = pd.to_datetime('now').to_period('m') - 12
df11 = df1[ df1.index.get_level_values('Date').to_period('m') == now]
print (df11)
col1 col2 col3 col4
Type Date
type1 2020-01-01 1.0 2.0 3.0 4.0
type2 2020-01-01 1.0 2.0 3.0 4.0
type3 2020-01-01 1.0 2.0 3.0 4.0
type4 2020-01-01 1.0 2.0 3.0 4.0
type5 2020-01-01 1.0 2.0 3.0 4.0
最后添加到第二个数据帧的concat
和排序MultiIndex
:
df3 = pd.concat([df11, df2]).sort_index()
print (df3)
col1 col2 col3 col4
Type Date
type1 2020-01-01 1.0 2.0 3.0 4.0
2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type2 2020-01-01 1.0 2.0 3.0 4.0
2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type3 2020-01-01 1.0 2.0 3.0 4.0
2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type4 2020-01-01 1.0 2.0 3.0 4.0
2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0
type5 2020-01-01 1.0 2.0 3.0 4.0
2021-01-01 10.0 20.0 30.0 40.0
2021-02-01 0.0 0.0 0.0 0.0
2021-03-01 0.0 0.0 0.0 0.0
2021-04-01 0.0 0.0 0.0 0.0
2021-05-01 0.0 0.0 0.0 0.0