python-2.7 - python2 Pandas : how to merge a part of another dataframe to a dataframe

标签 python-2.7 dataframe

我有一个数据框(df1)如下:

       datetime     m  d    1d    2d   3d
       2014-01-01   1  1     2     2   3
       2014-01-02   1  2     3     4   3
       2014-01-03   1  3     1     2   3
       ...........
       2014-12-01  12  1      2     2   3
       2014-12-31  12  31     2     2   3

我还有另一个数据框(df2),如下所示:

       datetime     m  d       
       2015-01-02   1  2     
       2015-01-03   1  3     
       ...........
       2015-12-01  12  1      
       2015-12-31  12  31     

我想将 df1 的 1d 2d 3d 列值合并到 df2。 有两个条件: (1) df1和df2中只有m和d相同才能合并。 (2) 如果 df2 index % 30 ==0 的索引不合并,则这些索引的 1d 2d 3d 的值可以为 Nan。

I mean I want the new dataframe of df2 like as following:

       datetime     m  d    1d    2d   3d
       2015-01-02   1  2   Nan     Nan   Nan
       2015-01-03   1  3     1     2   3
       ...........
       2015-12-01  12  1      2     2   3
       2015-12-31  12  31     2     2   3

提前致谢!

最佳答案

我认为您需要通过 loc 添加 NaN然后merge左连接:

np.random.seed(10)
N = 365
rng = pd.date_range('2015-01-01', periods=N)
df_tr_2014 = pd.DataFrame(np.random.randint(10, size=(N, 3)), index=rng).reset_index()
df_tr_2014.columns = ['datetime','7d','15d','20d']
df_tr_2014.insert(1,'month', df_tr_2014['datetime'].dt.month)
df_tr_2014.insert(2,'day_m', df_tr_2014['datetime'].dt.day)
#print (df_tr_2014.head())

N = 366
rng = pd.date_range('2016-01-01', periods=N)
df_te = pd.DataFrame(index=rng)
df_te['month'] = df_te.index.month
df_te['day_m'] = df_te.index.day
df_te = df_te.reset_index()
#print (df_te.tail())
df2 = df_te.copy()
df1 = df_tr_2014.copy()

df1 = df1.set_index('datetime')
df1.index += pd.offsets.DateOffset(years=1)

#correct 29 February
y = df1.index[0].year
df1 = df1.reindex(pd.date_range(pd.datetime(y,1,1), pd.datetime(y,12,31)))
idx = df1.index[(df1.index.month == 2) & (df1.index.day == 29)]

df1.loc[idx, :] = df1.loc[idx - pd.Timedelta(1, unit='d'), :].values
df1.loc[idx, 'day_m'] = idx.day
df1[['month','day_m']] = df1[['month','day_m']].astype(int)

df1[['7d','15d', '20d']] = df1[['7d','15d', '20d']].astype(float)

df1.loc[np.arange(len(df1.index))  % 30 == 0, ['7d','15d','20d']] = 0
df1 = df1.reset_index()
print (df1.iloc[57:62])
        index  month  day_m   7d  15d  20d
57 2016-02-27      2     27  2.0  0.0  1.0
58 2016-02-28      2     28  2.0  3.0  5.0
59 2016-02-29      2     29  2.0  3.0  5.0
60 2016-03-01      3      1  0.0  0.0  0.0
61 2016-03-02      3      2  7.0  6.0  9.0

关于python-2.7 - python2 Pandas : how to merge a part of another dataframe to a dataframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44752876/

相关文章:

python - 在 paramiko python 中的 sftp 期间更改了文件权限

python - 迭代数据框中的行,对每行执行计算并将答案放入新列中

python - 将数据帧列表附加到python中的数据帧列表

python - 从 pandas DataFrame 制作热图

最小元素的行名

python - 如何将数据重新组织到 pandas 中的新数据框中,以这种方式显示数据的更改?

python - 如何获得按输入顺序排序的 python Counter 输出?

python - isdigit 不适合我

c++ - 如何用 Cython 包装 C++ 类?

python - 连接中止。,Django 中的错误(104, 'Connection reset by peer')