![](/img/trans.png)
[英]Merge two columns from multiple panda series dataframes based on string matching from two columns with different values
[英]Matching two data different columns from two different dataframes
所以我有兩個不同的數據框,兩個數據框都有稱為“域”的列,並顯示來自不同來源的域。 域很干凈,看起來像www.google.com 、 www.facebook.com ,而不像www.facebook.com/4938249084 。
我要實現的目標是了解 DF1 中的任何域是否在 DF2 的域中,以及是否在 DF2 中,Append DF1 域是否在列表中。 這是我寫的代碼。
只是為了讓你們有一個想法,DF1 有大約 4,000 條記錄 v DF2 有大約 7,000 條記錄。
DF1
Domains
0 NaN
1 www.hawaiiantimeathletics.com
2 NaN
3 www.beach-elite.com
4 NaN
5 www.dreamingoldvbc.com
6 www.pacificunionvb.com
7 www.alabamajuniors.com
8 www.birminghamvolleyball.com
9 www.magiccitythunder.com
DF2
Domains
0 www.labcsandiego.com
1 www.ahavavolleyball.com
2 www.northernelite-va.com
3 www.theedgevc.com
4 divadallas.org
5 www.beach-elite.com
6 650xtremevolleyball.org
7 www.clubsouthvb.com
8 www.northidahovolleyball.com
9 wajvolleyball.site123.me
在此示例中,唯一應附加到列表中的域是“www.beach-elite.com”。
這是我寫的代碼
def match_domain(col1,col2):
list1 = []
for a in col1:
v1 = a
for b in col2:
v2 = b
if v1 == v2:
list1.append(v1)
print(v1)
elif v1 != v2:
print('none')
match_domain(DF1, DF2)
先感謝您 !
您可以使用isin
function 來確定這一點。
import pandas as pd
df1 = pd.DataFrame({'Domains': ['NaN', 'www.hawaiiantimeathletics.com', 'NaN', 'www.beach-elite.com', 'www.dreamingoldvbc.com', 'www.pacificunionvb.com', 'www.alabamajuniors.com', 'www.birminghamvolleyball.com', 'www.magiccitythunder.com']})
df2 = pd.DataFrame({'Domains': ['www.labcsandiego.com', 'www.ahavavolleyball.com', 'www.northernelite-va.com', 'www.theedgevc.com', 'divadallas.org', 'www.beach-elite.com', '650xtremevolleyball.org', 'www.clubsouthvb.com', 'www.northidahovolleyball.com', 'wajvolleyball.site123.me']})
df1.Domains[df1.Domains.isin(df2.Domains)].unique()
這應該給你域列的交集。
這可以使用列表理解來解決。 將兩個數據框轉換為列表並在另一個列表中查找,如下所示:-
df1:
Domains
NaN
www.hawaiiantimeathletics.com
NaN
www.beach-elite.com
NaN
www.dreamingoldvbc.com
www.pacificunionvb.com
www.alabamajuniors.com
www.birminghamvolleyball.com
www.magiccitythunder.com
df2:
Domains
www.labcsandiego.com
www.ahavavolleyball.com
www.northernelite-va.com
www.theedgevc.com
divadallas.org
www.beach-elite.com
650xtremevolleyball.org
www.clubsouthvb.com
www.northidahovolleyball.com
wajvolleyball.site123.me
代碼:
list1 = [ domain for domain in df1['Domains'].tolist() if domain in df2['Domains'].tolist() ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.