场景:我有2个数据帧,一个包含X期的数据,另一个包含期间X-1的数据(可能是日期或月份)。
数据示例:
DF1 (time X):
Item 1 Item 2
2018-01-02 0.431 0.656
2018-01-03 0.439 0.668
2018-01-04 0.447 0.680
2018-01-05 0.455 0.692
2018-01-08 0.479 0.729
2018-01-09 0.487 0.741
2018-01-10 0.495 0.753
DF1 (time X-1):
Item 1 Item 2
2018-01-02 0.429 0.651
2018-01-03 0.431 0.656
2018-01-04 0.439 0.668
2018-01-05 0.447 0.680
2018-01-08 0.455 0.692
2018-01-09 0.479 0.729
2018-01-10 0.487 0.741
我希望我的结果数据框为:
X Final:
Item 1 Item 2
2018-01-01 0.429 0.651
2018-01-02 0.431 0.656
2018-01-03 0.439 0.668
2018-01-04 0.447 0.680
2018-01-05 0.455 0.692
2018-01-08 0.479 0.729
2018-01-09 0.487 0.741
2018-01-10 0.495 0.753
我已经尝试了什么:我最初尝试将X数组的最后一列与x-1数组合并,并且偏移了索引,但是在这样做时,我也偏移了数据,不是我想要的:
df_xfinal = df_x1.iloc[:]
df_xlastrow = df_x.iloc[-1:]
df_xfinal = df_xfinal.append(df_xlastrow)
df_xfinal = df_xfinal.shift(periods=-1, axis=0)
问题:这可以直接完成,而不必遍历所有数组项吗?
答案 0 :(得分:1)
如果我理解正确:
public class DevSettings
{
public string DBConnectionString { get; set; }
public string DatabaseName { get; set; }
}
返回:
# Make sure the indices are datetimes (if they aren't already)
DF1.index = pd.to_datetime(DF1.index)
DF2.index = pd.to_datetime(DF2.index)
# subtract a day from the index of df2
DF2.index = DF2.index - pd.Timedelta('1d')
# concatenate the two dataframes, sort by index, and drop duplicates
final_df = pd.concat([DF1, DF2]).sort_index().drop_duplicates()