【问题标题】:pandas cross join no columns in common [duplicate]熊猫交叉加入没有共同的列[重复]
【发布时间】:2016-05-17 21:03:54
【问题描述】:

您将如何使用 pandas 对没有共同列的两个数据框执行 完全外连接 交叉连接

在 MySQL 中,您可以这样做:

SELECT *
FROM table_1
[CROSS] JOIN table_2;

但在 pandas 中,这样做:

df_1.merge(df_2, how='outer')

给出一个错误:

MergeError: No common columns to perform merge on

到目前为止我最好的解决方案是使用sqlite

将 sqlalchemy 导入为 sa 引擎 = sa.create_engine('sqlite:///tmp.db') df_1.to_sql('df_1', 引擎) df_2.to_sql('df_2', 引擎) df = pd.read_sql_query('SELECT * FROM df_1 JOIN df_2', engine)

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    即使在 MySQL 中,您也必须指定要加入哪些字段。

    http://dev.mysql.com/doc/refman/5.7/en/join.html

    例子:

    SELECT * FROM t1 LEFT JOIN t2 ON (t1.a = t2.a);
    

    与 Pandas 相同的概念:

    Parameters: 
    right : DataFrame
    how : {‘left’, ‘right’, ‘outer’, ‘inner’}, default ‘inner’
    left: use only keys from left frame (SQL: left outer join)
    right: use only keys from right frame (SQL: right outer join)
    outer: use union of keys from both frames (SQL: full outer join)
    inner: use intersection of keys from both frames (SQL: inner join)
    on : label or list
    Field names to join on. Must be found in both DataFrames. If on is None and not merging on indexes, then it merges on the intersection of the columns by default.
    left_on : label or list, or array-like
    Field names to join on in left DataFrame. Can be a vector or list of vectors of the length of the DataFrame to use a particular vector as the join key instead of columns
    right_on : label or list, or array-like
    Field names to join on in right DataFrame or vector/list of vectors per left_on docs
    

    http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html

    【讨论】:

    • 在 MySQL 中,ON 子句is optional。没有它,MySQL 将执行交叉连接。我将问题编辑得更清楚。
    【解决方案2】:

    IIUC 你需要mergetmp 的临时列DataFrames

    import pandas as pd
    
    df1 = pd.DataFrame({'fld1': ['x', 'y'],
                    'fld2': ['a', 'b1']})
    
    
    df2 = pd.DataFrame({'fld3': ['y', 'x', 'y'],
                    'fld4': ['a', 'b1', 'c2']})
    
    print df1
      fld1 fld2
    0    x    a
    1    y   b1
    
    print df2
      fld3 fld4
    0    y    a
    1    x   b1
    2    y   c2
    
    df1['tmp'] = 1
    df2['tmp'] = 1
    
    df = pd.merge(df1, df2, on=['tmp'])
    df = df.drop('tmp', axis=1)
    print df
      fld1 fld2 fld3 fld4
    0    x    a    y    a
    1    x    a    x   b1
    2    x    a    y   c2
    3    y   b1    y    a
    4    y   b1    x   b1
    5    y   b1    y   c2
    

    【讨论】:

    • 看起来像个 hack,但它确实有效!而且比 sqlite 快。希望有一种更清洁的方法来做到这一点...... :(
    • 这是enhancement的调用,所以在pandas的后续版本中可能会。
    猜你喜欢
    • 2017-03-26
    • 2021-12-19
    • 1970-01-01
    • 2021-04-09
    • 2021-05-11
    • 2016-03-13
    • 2016-09-23
    • 2017-01-16
    • 2016-04-23
    相关资源
    最近更新 更多