组合相似数据框的最佳方法是什么

我有 3 个df:


df_A:


    year  month day  A

0   2014    1   1   15.8

1   2014    1   2   21.0

2   2014    1   3   22.3

3   2014    1   4   20.2

4   2014    1   5   20.0

... ... ... ... ...

df_B:


    year  month day  B

0   2014    1   1   15.8

1   2014    1   2   21.0

2   2014    1   3   22.3

3   2014    1   4   20.2

4   2014    1   5   20.0

... ... ... ... ...

df_C:


    year  month day  C

0   2014    1   1   15.8

1   2014    1   2   21.0

2   2014    1   3   22.3

3   2014    1   4   20.2

4   2014    1   5   20.0

... ... ... ... ...

我想 1) 并排加入他们;2)将3个日期列合二为一,如下所示:


     date        A    B    C 

0   2014-1-1    15.8 15.8 15.8

1   2014-1-2    21.0 21.0 21.0

2   2014-1-3    22.3 22.3 22.3

3   2014-1-4    20.2 20.2 20.2

4   2014-1-5    20.0 20.0 20.0

... ... ... ... ...

我试过了


df_A['date']=pd.to_datetime(df_A[['year','month','day']])

但它回来了


---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-12-88de4e50b4f6> in <module>()

----> 1 df_A['date']=pd.to_datetime(df_A[['year','month','day']])

      2 

      3 


3 frames

/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)

   1183             if not (self.name == "loc" and not raise_missing):

   1184                 not_found = list(set(key) - set(ax))

-> 1185                 raise KeyError("{} not in index".format(not_found))

   1186 

   1187           


KeyError: "['year'] not in index"

做这个的最好方式是什么?


交互式爱情
浏览 123回答 2
2回答

Qyouu

IIUC,我们可以使用一个函数来清理你的日期,然后沿着轴 =1 连接def create_datetime(dataframe, year="year", month="month", day="day"):&nbsp; &nbsp; dataframe["year"] = pd.to_datetime(&nbsp; &nbsp; &nbsp; &nbsp; df[year].astype(str) + "-" + df[month].astype(str) + "-" + df[day].astype(str),&nbsp; &nbsp; &nbsp; &nbsp; format="%Y-%m-%d",&nbsp; &nbsp; )&nbsp; &nbsp; dataframe = dataframe.drop([month, day], axis=1)&nbsp; &nbsp; dataframe = dataframe.rename(columns={year : 'date'})&nbsp; &nbsp; dataframe = dataframe.set_index('date')&nbsp; &nbsp; return dataframedfA = create_datetime(dfA)dfB = create_datetime(dfB)dfC = create_datetime(dfC)final = pd.concat([dfA,dfB,dfC],axis=1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A&nbsp; &nbsp; &nbsp;C&nbsp; &nbsp; &nbsp;Cdate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;2014-01-01&nbsp; 15.8&nbsp; 15.8&nbsp; 15.82014-01-02&nbsp; 21.0&nbsp; 21.0&nbsp; 21.02014-01-03&nbsp; 22.3&nbsp; 22.3&nbsp; 22.32014-01-04&nbsp; 20.2&nbsp; 20.2&nbsp; 20.22014-01-05&nbsp; 20.0&nbsp; 20.0&nbsp; 20.0

慕尼黑8549860

尝试:df_result = ((df.set_index(['year','month','day'])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.join([df1.set_index(['year','month','day']),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; df2.set_index(['year','month','day'])]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.reset_index())df_result['date'] = pd.to_datetime((df.year*10000+df.month*100+df.day).apply(str),format='%Y%m%d')df_result.drop(['year','month','day'], axis=1, inplace=True)df_result&nbsp; &nbsp; &nbsp; A&nbsp; &nbsp; &nbsp;B&nbsp; &nbsp; &nbsp;C&nbsp; &nbsp; &nbsp; &nbsp;date0&nbsp; 15.8&nbsp; 15.8&nbsp; 15.8 2014-01-011&nbsp; 21.0&nbsp; 21.0&nbsp; 21.0 2014-01-022&nbsp; 22.3&nbsp; 22.3&nbsp; 22.3 2014-01-033&nbsp; 20.2&nbsp; 20.2&nbsp; 20.2 2014-01-044&nbsp; 20.0&nbsp; 20.0&nbsp; 20.0 2014-01-05
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python