Pandas:合并两个具有不同索引和缺失值的数据帧

时间:2018-03-13 03:41:31

标签: python-3.x pandas indexing pandas-groupby

我正在尝试将两个数据帧合并在一起。 df2的样本点数多于df。我想基于df的索引合并它们,其方式是每个时间戳的最接近的非缺失值到时间戳是值。

我的原始数据集是分类的,这就是我将列作为字符串的原因。

from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import random
    ##Generate the Data
np.random.seed(12) 
date_today = datetime.now()
ndays = 5
df = pd.DataFrame({'date': [date_today + timedelta(days=x) for x in range(ndays)], 
                       'test': pd.Series(np.random.randn(ndays)),     'test2':pd.Series(np.random.randn(ndays))})
df = df.set_index('date').sort_index()
df = df.mask(np.random.random(df.shape) < .7)
print(df)

df2 = pd.DataFrame({'date': [date_today + timedelta(days=(abs(np.random.randn(1))*0.25)[0]*x) for x in range(ndays*2)], 
                       'test3': pd.Series(np.random.randn(ndays*2))})
df2 = df2.set_index('date').sort_index() 

df2 = df2.mask(np.random.random(df2.shape) < .3)
df['test']=df['test'].astype(str)
df['test2']=df['test2'].astype(str)
df2['test3']=df2['test3'].astype(str)



print(df2)
df2.reindex(df.index, method='bfill')

当前输出:

                                test3
date    
2018-03-12 22:31:52.177918  -1.6817565103951275
2018-03-13 22:31:52.177918  nan
2018-03-14 22:31:52.177918  nan
2018-03-15 22:31:52.177918  nan
2018-03-16 22:31:52.177918  nan

渴望出局:

                                test3
date    
2018-03-12 22:31:52.177918  -1.6817565103951275
2018-03-13 22:31:52.177918   0.214975948415751
2018-03-14 22:31:52.177918  nan
2018-03-15 22:31:52.177918  nan
2018-03-16 22:31:52.177918  nan

提前致谢,

1 个答案:

答案 0 :(得分:2)

使用method='nearest'

中的reindex参数设置
df2.reindex(df.index, method='nearest')

date                                      
2018-03-12 20:44:02.753549   -1.6817565104
2018-03-13 20:44:02.753549  0.214975948416
2018-03-14 20:44:02.753549             nan
2018-03-15 20:44:02.753549             nan
2018-03-16 20:44:02.753549             nan