zoukankan      html  css  js  c++  java
  • python dataframe 在merge时 产生笛卡尔积

    在pandas中,concat, merge, join的使用方法可以参考以下资料:
    http://blog.csdn.net/stevenkwong/article/details/52528616
    主要讲下笛卡尔积:

    import pandas as pd
    from pandas import DataFrame
    df1=DataFrame({'a':[1,2,3], 'b':[4,5,6], 'key':[0,0,0]})
    df2=DataFrame({'c':[3,2,1], 'd':[6,5,4], 'key':[0,0,0]})
    data = pd.merge(df1, df2, on='key')
    

    这里merge默认为内连接。

    df1:

       a  b  key
    0  1  4    0
    1  2  5    0
    2  3  6    0
    

      

    df2:

       c  d  key
    0  3  6    0
    1  2  5    0
    2  1  4    0
    

      

    data:

       a  b  key  c  d
    0  1  4    0  3  6
    1  1  4    0  2  5
    2  1  4    0  1  4
    3  2  5    0  3  6
    4  2  5    0  2  5
    5  2  5    0  1  4
    6  3  6    0  3  6
    7  3  6    0  2  5
    8  3  6    0  1  4
    

      

    由此可知,当两个表连接时,有相同的key值就产生积。

    如果,需要进行merge的次数过多时,每次都产生笛卡尔积,最终就会产生内存爆炸的现象。

    所以,在merge时,一定要避免相同的key值,可以分批次merge,最后再concat。
    ---------------------

    原文:https://blog.csdn.net/yj1556492839/article/details/79529186

  • 相关阅读:
    文件的上传下载
    HttpServletResponse
    HttpServletRequest
    web工程中URL地址的推荐写法
    servlet二
    Servlet
    HTTP-崔希凡笔记
    HTTP协议-引自孤傲苍狼博客
    浏览器与服务器交互的过程
    Tomcat 配置
  • 原文地址:https://www.cnblogs.com/Allen-rg/p/9848270.html
Copyright © 2011-2022 走看看