使用pandas 0.15.1。假设我有以下两个数据帧:
daily
2014-11-20 00:00:00 Rain
2014-11-21 00:00:00 Cloudy
2014-11-22 00:00:00 Sunny
minutely
2014-11-20 12:45:00 51
2014-11-20 12:46:00 43
2014-11-20 12:47:00 44
...
2014-11-21 12:45:00 44
2014-11-21 12:46:00 46
2014-11-21 12:47:00 48
...
2014-11-22 12:45:00 38
2014-11-22 12:46:00 32
2014-11-22 12:47:00 37
我想合并两个数据框,以便将日值传播到具有相应日期的每分钟行。
由于分钟行在00:00:00实际上没有数据,所以我不希望在结果数据帧中包含该时间。期望的输出:
2014-11-20 12:45:00 51 Rain
2014-11-20 12:46:00 43 Rain
2014-11-20 12:47:00 44 Rain
...
2014-11-21 12:45:00 44 Cloudy
2014-11-21 12:46:00 46 Cloudy
2014-11-21 12:47:00 48 Cloudy
...
2014-11-22 12:45:00 38 Sunny
2014-11-22 12:46:00 32 Sunny
2014-11-22 12:47:00 37 Sunny
我怎样才能做到这一点?我需要使用merge,concat还是join?
答案 0 :(得分:3)
从:
开始>>> left
minutely
2014-11-20 12:45:00 51
2014-11-20 12:46:00 43
2014-11-20 12:47:00 44
2014-11-21 12:45:00 44
2014-11-21 12:46:00 46
2014-11-21 12:47:00 48
2014-11-22 12:45:00 38
2014-11-22 12:46:00 32
2014-11-22 12:47:00 37
>>> right
daily
2014-11-20 Rain
2014-11-21 Cloudy
2014-11-22 Sunny
你可能会这样做:
>>> left['day'] = left.index.date
>>> right.index = right.index.date
>>> left.join(right, on='day', how='left')
minutely day daily
2014-11-20 12:45:00 51 2014-11-20 Rain
2014-11-20 12:46:00 43 2014-11-20 Rain
2014-11-20 12:47:00 44 2014-11-20 Rain
2014-11-21 12:45:00 44 2014-11-21 Cloudy
2014-11-21 12:46:00 46 2014-11-21 Cloudy
2014-11-21 12:47:00 48 2014-11-21 Cloudy
2014-11-22 12:45:00 38 2014-11-22 Sunny
2014-11-22 12:46:00 32 2014-11-22 Sunny
2014-11-22 12:47:00 37 2014-11-22 Sunny