慕村225694
鉴于描述,我建议使用pd.concator merge。这是一个测试示例:import pandas as pd#generating test dataindex1 = pd.date_range('1/1/2000', periods=9, freq='D')index2 = pd.date_range('1/4/2000', periods=9, freq='D')series = range(9)df1 = pd.DataFrame([index1,series]).Tdf2 = pd.DataFrame([index2,series]).Tdf1.columns = ['Time','Data']df2.columns = ['Time','Data']df1: Time Data0 2000-01-01 00:00:00 01 2000-01-02 00:00:00 12 2000-01-03 00:00:00 23 2000-01-04 00:00:00 34 2000-01-05 00:00:00 45 2000-01-06 00:00:00 56 2000-01-07 00:00:00 67 2000-01-08 00:00:00 78 2000-01-09 00:00:00 8 df2: Time Data0 2000-01-04 00:00:00 01 2000-01-05 00:00:00 12 2000-01-06 00:00:00 23 2000-01-07 00:00:00 34 2000-01-08 00:00:00 45 2000-01-09 00:00:00 56 2000-01-10 00:00:00 67 2000-01-11 00:00:00 78 2000-01-12 00:00:00 8请注意,两个数据框中的数据可用于不同的日期。#convert Time to pandas datetime format#df1['Time'].to_datetime(df1['Time']) # <- uncomment this for your case#df1['Time'].to_datetime(df1['Time']) # <- uncomment this for your case#making the time the index of the dataframesdf1.set_index(['Time'],inplace=True)df2.set_index(['Time'],inplace=True)#concatenating the dataframe column wise (axis=1)df3 = pd.concat([df1,df2],axis=1)print(df3)输出: Data DataTime 2000-01-01 0 NaN2000-01-02 1 NaN2000-01-03 2 NaN2000-01-04 3 02000-01-05 4 12000-01-06 5 22000-01-07 6 32000-01-08 7 42000-01-09 8 52000-01-10 NaN 62000-01-11 NaN 72000-01-12 NaN 8处理缺失值:pd.concat correctly merges the data as per the data. NaN indicate the missing values after combining, which can be handled mainly with fillna(filling something inplace of NaN) or dropna (dropping the data containing NaN). Here is an example of fillna (dropna is used exactly the same way but without 0) :#filling 0's inplace of `NaN`. You can use also method='bfill' or 'ffill' or interpolatedf3 = df3.fillna(0,inplace=True) #df3 = df3.fillna(method='bfill',inplace=True) # <- uncomment if you want to use this#df3 = df3.fillna(method='ffill',inplace=True) # <- uncomment if you want to use thisOutput: Data DataTime 2000-01-01 0 02000-01-02 1 02000-01-03 2 02000-01-04 3 02000-01-05 4 12000-01-06 5 22000-01-07 6 32000-01-08 7 42000-01-09 8 52000-01-10 0 62000-01-11 0 72000-01-12 0 8