答案 0 :(得分:4)
index = pd.MultiIndex.from_product(
[df1['symbol'], df2['symbol']],
names=['col1', 'col2'])
pd.DataFrame(index=index).reset_index()
col1 col2
0 L160 K-AL
1 L160 K-PL
2 L170 K-AL
3 L170 K-PL
4 L180 K-AL
5 L180 K-PL
编辑:
现在有了@Andy L.,情况会更好:
pd.MultiIndex.from_product([df1['symbol'], df2['symbol']], names=['col1', 'col2']) \
.to_frame(index=False)
答案 1 :(得分:3)
Assign
的人工键列及其上的merge
,以获取所有匹配项:
df1.assign(key=1).merge(df2.assign(key=1), on='key', suffixes=['_1', '_2']).drop('key', axis=1)
输出
symbol_1 symbol_2
0 L160 K-AL
1 L160 K-PL
2 L170 K-AL
3 L170 K-PL
4 L180 K-AL
5 L180 K-PL
答案 2 :(得分:2)
df1 = DataFrame({'key':[1,1,1],'symbol':['L160','L170','L180']})
df2 = DataFrame({'key':[1,1],'symbol':['K-PL','K-AL']})
merge(df1, df2,on='key')[['col1', 'col2']]
基本上是笛卡尔积。 LINK
输出
key symbol_x symbol_y
0 1 L160 K-PL
1 1 L160 K-AL
2 1 L170 K-PL
3 1 L170 K-AL
4 1 L180 K-PL
5 1 L180 K-AL
答案 3 :(得分:1)
使用Numpy也可以:
import numpy as np
import pandas as pd
df1 = pd.DataFrame(['L160', 'L170', 'L180'],columns=['symbol'])
df2 = pd.DataFrame(['K-AL', 'K-PL'],columns=['symbol'])
np.transpose([np.tile(df1['symbol'], len(df2['symbol'])), np.repeat(df2['symbol'], len(df1['symbol']))])
或直接在Pandas数据框中:
pd.DataFrame(np.transpose([np.tile(df1['symbol'], len(df2['symbol'])), np.repeat(df2['symbol'], len(df1['symbol']))]))