如何在python中连接两个数据帧

时间:2018-04-24 12:34:48

标签: python python-3.x pandas dataframe

我有两个数据框,我想加入它们,以便我可以在一个数据框中检查每一周的数量。


2018-04-24 11:59:24,620 [ajp-apr-8009-exec-9] [ERROR] (o.a.s.a.AccessServiceImpl:1070) - Error during access server backup
org.jfrog.access.client.AccessClientHttpException: HTTP response status 401:Failed on executing /api/v1/system/backup/export, with response: {"errors":[{"code":"UNAUTHORIZED","detail":"Bad credentials","message":"HTTP 401 Unauthorized"}]}
        at org.jfrog.access.client.http.AccessHttpClient.createRestResponse(AccessHttpClient.java:154)
        at org.jfrog.access.client.http.AccessHttpClient.restCall(AccessHttpClient.java:113)
        at org.jfrog.access.client.system.SystemClientImpl.exportAccessServer(SystemClientImpl.java:21)
        at org.artifactory.security.access.AccessServiceImpl.exportTo(AccessServiceImpl.java:1060)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
        at com.sun.proxy.$Proxy144.exportTo(Unknown Source)
        at org.artifactory.spring.ArtifactoryApplicationContext.exportTo(ArtifactoryApplicationContext.java:662)
        at org.artifactory.ui.rest.service.admin.importexport.exportdata.ExportSystemService.execute(ExportSystemService.java:67)
        at org.artifactory.rest.common.service.ServiceExecutor.process(ServiceExecutor.java:38)
        at org.artifactory.rest.common.resource.BaseResource.runService(BaseResource.java:92)
        at org.artifactory.ui.rest.resource.admin.importexport.ExportArtifactResource.exportSystem(ExportArtifactResource.java:65)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
        at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
        at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
        at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
        at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
        at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
        at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
        at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
        at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
        at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
        at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
        at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.artifactory.webapp.servlet.RepoFilter.execute(RepoFilter.java:184)
        at org.artifactory.webapp.servlet.RepoFilter.doFilter(RepoFilter.java:93)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.artifactory.webapp.servlet.AccessFilter.useAuthentication(AccessFilter.java:403)
        at org.artifactory.webapp.servlet.AccessFilter.doFilterInternal(AccessFilter.java:212)
        at org.artifactory.webapp.servlet.AccessFilter.doFilter(AccessFilter.java:166)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.artifactory.webapp.servlet.RequestFilter.doFilter(RequestFilter.java:67)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:164)
        at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:80)
        at org.artifactory.webapp.servlet.SessionFilter.doFilter(SessionFilter.java:62)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.artifactory.webapp.servlet.ArtifactoryFilter.doFilter(ArtifactoryFilter.java:128)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
        at org.apache.coyote.ajp.AbstractAjpProcessor.process(AbstractAjpProcessor.java:877)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2527)
        at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2516)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)
2018-04-24 11:59:24,660 [ajp-apr-8009-exec-9] [INFO ] (o.a.s.ArtifactoryApplicationContext:819) - Note: the etc exported folder has excessive permissions. Be careful with the files.

我想加入两个数据框,如结果所示,我想在一个数据框中追加每个城市每周的数量。

df1=  City  Week        qty       Year
      hyd   35          10        2015
      hyd   36          15        2015
      hyd   37          11        2015
      hyd   42          10        2015
      hyd   23          10        2016
      hyd   32          15        2016
      hyd   37          11        2017
      hyd   42          10        2017
      pune  35          10        2015
      pune  36          15        2015
      pune  37          11        2015
      pune  42          10        2015
      pune  23          10        2016
      pune  32          15        2016
      pune  37          11        2017
      pune  42          10        2017

 df2= city  Week         qty       Year
      hyd   23          10        2015
      hyd   32          15        2015
      hyd   35          12        2016
      hyd   36          15        2016
      hyd   37          11        2016
      hyd   42          10        2016
      hyd   43          12        2016
      hyd   44          18        2016
      hyd   35          11        2017
      hyd   36          15        2017
      hyd   37          11        2017
      hyd   42          10        2017
      hyd   51          14        2017
      hyd   52          17        2017
      pune  35          12        2016
      pune  36          15        2016
      pune  37          11        2016
      pune  42          10        2016
      pune  43          12        2016
      pune  44          18        2016
      pune  35          11        2017
      pune  36          15        2017
      pune  37          11        2017
      pune  42          10        2017
      pune  51          14        2017
      pune  52          17        2017

2 个答案:

答案 0 :(得分:1)

您可以将您的任务分解为几个步骤:

  1. 合并您的数据框df1df2
  2. 根据合并后的数据框架创建数据框列表,按年份拆分。
  3. 同时,重命名列以反映年份,将索引设置为Week
  4. 最后,连接axis=1reset_index
  5. 以下是一个例子:

    df = pd.concat([df1, df2], ignore_index=True)
    
    dfs = [df[df['Year'] == y].rename(columns=lambda x: x+'_'+str(y) if x != 'Week' else x)\
                              .set_index('Week') for y in df['Year'].unique()]
    
    res = pd.concat(dfs, axis=1).reset_index()
    

    结果:

    print(res)
    
       Week  qty_2015  Year_2015  qty_2016  Year_2016  qty_2017  Year_2017
    0    35      10.0     2015.0      12.0     2016.0      11.0     2017.0
    1    36      15.0     2015.0      15.0     2016.0      15.0     2017.0
    2    37      11.0     2015.0      11.0     2016.0      11.0     2017.0
    3    42      10.0     2015.0      10.0     2016.0      10.0     2017.0
    4    43       NaN        NaN      12.0     2016.0       NaN        NaN
    5    44       NaN        NaN      18.0     2016.0       NaN        NaN
    6    51       NaN        NaN       NaN        NaN      14.0     2017.0
    7    52       NaN        NaN       NaN        NaN      17.0     2017.0
    

答案 1 :(得分:1)

就我个人而言,我认为您的示例输出是可读的,因此除非您因特定原因需要该格式,否则我可能会考虑使用数据透视表。我还认为所需的代码更清晰。

import pandas as pd 

df3 = pd.concat([df1, df2], ignore_index=True)
df4 = df3.pivot(index='Week', columns='Year', values='qty')

print(df4)

Year  2015  2016  2017
Week                  
35    10.0  12.0  11.0
36    15.0  15.0  15.0
37    11.0  11.0  11.0
42    10.0  10.0  10.0
43     NaN  12.0   NaN
44     NaN  18.0   NaN
51     NaN   NaN  14.0
52     NaN   NaN  17.0