按时和另一列合并pandas数据帧

时间:2016-12-21 04:48:47

标签: python pandas merge

我有两个pandas数据帧,我试图将它们组合成一个数据帧。以下是我如何设置它们的方法:

a = {'date':['1/1/2015 00:00','1/1/2015 00:15','1/1/2015 00:30'], 'num':[1,2,3]}
b = {'date':['1/1/2015 01:15','1/1/2015 01:30','1/1/2015 01:45'], 'num':[4,5,6]}

dfa = pd.DataFrame(a)
dfb = pd.DataFrame(b)

dfa['date'] = dfa['date'].apply(pd.to_datetime)
dfb['date'] = dfb['date'].apply(pd.to_datetime)

然后,我会从每个时间戳中找到earliestlatest时间戳,并创建一个仅以date系列开头的新数据框:

earliest = min(dfa['date'].min(), dfb['date'].min())
latest = max(dfa['date'].max(), dfb['date'].max())

date_range = pd.date_range(earliest, latest, freq='15min')

dfd = pd.DataFrame({'date':date_range})

然后我想将它们全部合并到一个数据框中,dfd作为基础,因为它将包含所有正确的时间戳。所以我合并了dfddfa,一切都很好:

dfd = pd.merge(dfd, dfa, how = 'outer', on = 'date')

然而,当我将其与dfb合并时,date系列变得棘手,我无法弄清楚原因。

dfd = pd.merge(dfd, dfb, how = 'outer', on = ['date','num'])

...产率:

                  date  num
0  2015-01-01 00:00:00  1.0
1  2015-01-01 00:15:00  2.0
2  2015-01-01 00:30:00  3.0
3  2015-01-01 00:45:00  NaN
4  2015-01-01 01:00:00  NaN
5  2015-01-01 01:15:00  NaN
6  2015-01-01 01:30:00  NaN
7  2015-01-01 01:45:00  NaN
8  2015-01-01 01:15:00  4.0
9  2015-01-01 01:30:00  5.0
10 2015-01-01 01:45:00  6.0

我希望4.0填写2015-01-01 01:15:00时段等,而不是创建新行。

或者如果我尝试:

dfd = pd.merge(dfd, dfb, how = 'outer', on = 'date')

我明白了:

                 date  num_x  num_y
0 2015-01-01 00:00:00    1.0    NaN
1 2015-01-01 00:15:00    2.0    NaN
2 2015-01-01 00:30:00    3.0    NaN
3 2015-01-01 00:45:00    NaN    NaN
4 2015-01-01 01:00:00    NaN    NaN
5 2015-01-01 01:15:00    NaN    4.0
6 2015-01-01 01:30:00    NaN    5.0
7 2015-01-01 01:45:00    NaN    6.0

这也不是我想要的(只需要一个num列)。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

dfa.set_index('date').combine_first(dfb.set_index('date')) \
    .asfreq('15T').reset_index()

                 date    num
0 2015-01-01 00:00:00 1.0000
1 2015-01-01 00:15:00   2.00
2 2015-01-01 00:30:00   3.00
3 2015-01-01 00:45:00    nan
4 2015-01-01 01:00:00    nan
5 2015-01-01 01:15:00   4.00
6 2015-01-01 01:30:00   5.00
7 2015-01-01 01:45:00   6.00

另一种解决方案

dfa.append(dfb).set_index('date').asfreq('15T').reset_index()

答案 1 :(得分:1)

首先合并dfa和dfb:

d = pd.merge(dfa, dfb, on=['date','num'], how='outer')

然后将结果与你定义的dfd结合起来:

result = pd.merge(d, dfd, on='date', how='outer')
print result.sort('date')

输出:

                 date  num
0 2015-01-01 00:00:00  1.0
1 2015-01-01 00:15:00  2.0
2 2015-01-01 00:30:00  3.0
6 2015-01-01 00:45:00  NaN
7 2015-01-01 01:00:00  NaN
3 2015-01-01 01:15:00  4.0
4 2015-01-01 01:30:00  5.0
5 2015-01-01 01:45:00  6.0

答案 2 :(得分:0)

这有效:

a = {'date':['1/1/2015 00:00','1/1/2015 00:15','1/1/2015 00:30'], 'num':[1,2,3]}
b = {'date':['1/1/2015 01:15','1/1/2015 01:30','1/1/2015 01:45'], 'num':[4,5,6]}

dfa = pd.DataFrame(a)
dfb = pd.DataFrame(b)

dfa['date'] = dfa['date'].apply(pd.to_datetime)
dfb['date'] = dfb['date'].apply(pd.to_datetime)

earliest = min(dfa['date'].min(), dfb['date'].min())
latest = max(dfa['date'].max(), dfb['date'].max())

date_range = pd.date_range(earliest, latest, freq='15min')

dfd = pd.DataFrame({'date':date_range})


df_dates = pd.merge(dfa, dfb, how = 'outer')
df_final = pd.merge(dfd, df_dates, how = 'outer')

df_final