python - pandas cross join no columns in common -
how perform full outer join a cross join of 2 dataframes no columns in common using pandas?
in mysql, can do:
select * table_1 [cross] join table_2;
but in pandas, doing:
df_1.merge(df_2, how='outer')
gives error:
mergeerror: no common columns perform merge on
the best solution have far using sqlite
:
import sqlalchemy sa engine = sa.create_engine('sqlite:///tmp.db') df_1.to_sql('df_1', engine) df_2.to_sql('df_2', engine) df = pd.read_sql_query('select * df_1 join df_2', engine)
iiuc need merge
temporary columns tmp
of both dataframes
:
import pandas 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 1 y b1 print df2 fld3 fld4 0 y 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 y 1 x x b1 2 x y c2 3 y b1 y 4 y b1 x b1 5 y b1 y c2
Comments
Post a Comment