为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

交互式爱情 2022-07-26 15:42:26
我有 3 个df:df_A:    year  month day  A0   2014    1   1   15.81   2014    1   2   21.02   2014    1   3   22.33   2014    1   4   20.24   2014    1   5   20.0... ... ... ... ...df_B:    year  month day  B0   2014    1   1   15.81   2014    1   2   21.02   2014    1   3   22.33   2014    1   4   20.24   2014    1   5   20.0... ... ... ... ...df_C:    year  month day  C0   2014    1   1   15.81   2014    1   2   21.02   2014    1   3   22.33   2014    1   4   20.24   2014    1   5   20.0... ... ... ... ...我想 1) 并排加入他们;2)将3个日期列合二为一,如下所示:     date        A    B    C 0   2014-1-1    15.8 15.8 15.81   2014-1-2    21.0 21.0 21.02   2014-1-3    22.3 22.3 22.33   2014-1-4    20.2 20.2 20.24   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"做这个的最好方式是什么?
查看完整描述

2 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

IIUC,我们可以使用一个函数来清理你的日期,然后沿着轴 =1 连接


def create_datetime(dataframe, year="year", month="month", day="day"):

    dataframe["year"] = pd.to_datetime(

        df[year].astype(str) + "-" + df[month].astype(str) + "-" + df[day].astype(str),

        format="%Y-%m-%d",

    )

    dataframe = dataframe.drop([month, day], axis=1)

    dataframe = dataframe.rename(columns={year : 'date'})

    dataframe = dataframe.set_index('date')

    return dataframe

dfA = create_datetime(dfA)


dfB = create_datetime(dfB)


dfC = create_datetime(dfC)


final = pd.concat([dfA,dfB,dfC],axis=1)


               A     C     C

date                        

2014-01-01  15.8  15.8  15.8

2014-01-02  21.0  21.0  21.0

2014-01-03  22.3  22.3  22.3

2014-01-04  20.2  20.2  20.2

2014-01-05  20.0  20.0  20.0


查看完整回答
反对 回复 2022-07-26
?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

尝试:


df_result = ((df.set_index(['year','month','day'])

               .join([df1.set_index(['year','month','day']), 

                      df2.set_index(['year','month','day'])]))

               .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


      A     B     C       date

0  15.8  15.8  15.8 2014-01-01

1  21.0  21.0  21.0 2014-01-02

2  22.3  22.3  22.3 2014-01-03

3  20.2  20.2  20.2 2014-01-04

4  20.0  20.0  20.0 2014-01-05


查看完整回答
反对 回复 2022-07-26
  • 2 回答
  • 0 关注
  • 121 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号