[英]Match DataFrame column value against another DataFrame column and count hits
[英]PySpark: match the values of a DataFrame column against another DataFrame column
在Pandas DataFrame中,我可以使用DataFrame.isin()
函數將列值與另一列匹配。
例如:假設我們有一個DataFrame:
df_A = pd.DataFrame({'col1': ['A', 'B', 'C', 'B', 'C', 'D'],
'col2': [1, 2, 3, 4, 5, 6]})
df_A
col1 col2
0 A 1
1 B 2
2 C 3
3 B 4
4 C 5
5 D 6
和另一個DataFrame:
df_B = pd.DataFrame({'col1': ['C', 'E', 'D', 'C', 'F', 'G', 'H'],
'col2': [10, 20, 30, 40, 50, 60, 70]})
df_B
col1 col2
0 C 10
1 E 20
2 D 30
3 C 40
4 F 50
5 G 60
6 H 70
我可以使用.isin()
函數將df_B
的列值與df_A
的列值進行df_A
例如:
df_B[df_B['col1'].isin(df_A['col1'])]
收益率:
col1 col2
0 C 10
2 D 30
3 C 40
PySpark DataFrame中的等效操作是什么?
df_A = pd.DataFrame({'col1': ['A', 'B', 'C', 'B', 'C', 'D'],
'col2': [1, 2, 3, 4, 5, 6]})
df_A = sqlContext.createDataFrame(df_A)
df_B = pd.DataFrame({'col1': ['C', 'E', 'D', 'C', 'F', 'G', 'H'],
'col2': [10, 20, 30, 40, 50, 60, 70]})
df_B = sqlContext.createDataFrame(df_B)
df_B[df_B['col1'].isin(df_A['col1'])]
上面的.isin()
代碼給出了一條錯誤消息:
u'resolved attribute(s) col1#9007 missing from
col1#9012,col2#9013L in operator !Filter col1#9012 IN
(col1#9007);;\n!Filter col1#9012 IN (col1#9007)\n+-
LogicalRDD [col1#9012, col2#9013L]\n'
這種操作在spark中稱為左半連接:
df_B.join(df_A, ['col1'], 'leftsemi')
過濾器是如何選擇行的,isin運算符是相同的
df.filter(col('col1').isin(highf['col1']))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.