Skip to main content
 首页 » 编程设计

python之用另一个数据框中的匹配 ID 替换 Pandas 中的单元格值

2025年05月04日15bhlsheji

我有一个数据框,其中包含我通过 pandas 库存储的域列表(在我的例子中是顶点/节点):

                 domain 
0            airbnb.com 
1          facebook.com 
2                st.org 
3              index.co 
4        crunchbase.com 
5               avc.com 
6        techcrunch.com 
7            google.com 

我有另一个数据框,其中包含这些域(也称为边)之间的连接:

           source_domain    destination_domain 
0             airbnb.com            google.com 
1           facebook.com            google.com 
2                 st.org          facebook.com 
3                 st.org            airbnb.com 
4                 st.org        crunchbase.com 
5               index.co        techcrunch.com 
6         crunchbase.com        techcrunch.com 
7         crunchbase.com            airbnb.com 
8                avc.com        techcrunch.com 
9         techcrunch.com                st.org 
10        techcrunch.com            google.com 
11        techcrunch.com          facebook.com 

因为这个数据集会变得更大,我读到如果我只用整数而不是字符串来表示“边缘”数据帧,我可以获得更快的性能。

所以,我想知道是否有一种快速的方法可以将边缘数据框中的每个单元格替换为域(也称为顶点)数据框中的相应 ID?所以边缘数据框中的第 1 行最终可能看起来像:

###### Before: #####################  
1           facebook.com google.com    
###### After:  #####################    
1           1            7 

我该怎么做呢?提前谢谢你。

请您参考如下方法:

我尝试实现另一个答案 - 转换为 Catagorical 并为 ints 使用 cat.codes :

#if always unique domain in df1 can be omit 
#cats = df1['domain'].unique() 
cats = df1['domain'] 
df2['source_domain'] = df2['source_domain'].astype('category', categories=cats) 
df2['destination_domain'] = df2['destination_domain'].astype('category', categories=cats) 
df2['source_code'] = df2['source_domain'].cat.codes 
df2['dest_code'] = df2['destination_domain'].cat.codes 
print (df2) 
     source_domain destination_domain  source_code  dest_code 
0       airbnb.com         google.com            0          7 
1     facebook.com         google.com            1          7 
2           st.org       facebook.com            2          1 
3           st.org         airbnb.com            2          0 
4           st.org     crunchbase.com            2          4 
5         index.co     techcrunch.com            3          6 
6   crunchbase.com     techcrunch.com            4          6 
7   crunchbase.com         airbnb.com            4          0 
8          avc.com     techcrunch.com            5          6 
9   techcrunch.com             st.org            6          2 
10  techcrunch.com         google.com            6          7 
11  techcrunch.com       facebook.com            6          1 

df2['source_domain'] = df2['source_domain'].astype('category', categories=cats).cat.codes 
df2['destination_domain'] = df2['destination_domain'].astype('category', categories=cats) 
                                                     .cat.codes 
print (df2) 
    source_domain  destination_domain 
0               0                   7 
1               1                   7 
2               2                   1 
3               2                   0 
4               2                   4 
5               3                   6 
6               4                   6 
7               4                   0 
8               5                   6 
9               6                   2 
10              6                   7 
11              6                   1 

如果想用dict 替换使用map :

d = dict(zip(df1.domain.values, df1.index.values)) 
df2['source_code'] = df2['source_domain'].map(d) 
df2['dest_code'] = df2['destination_domain'].map(d) 
print (df2) 
     source_domain destination_domain  source_code  dest_code 
0       airbnb.com         google.com            0          7 
1     facebook.com         google.com            1          7 
2           st.org       facebook.com            2          1 
3           st.org         airbnb.com            2          0 
4           st.org     crunchbase.com            2          4 
5         index.co     techcrunch.com            3          6 
6   crunchbase.com     techcrunch.com            4          6 
7   crunchbase.com         airbnb.com            4          0 
8          avc.com     techcrunch.com            5          6 
9   techcrunch.com             st.org            6          2 
10  techcrunch.com         google.com            6          7 
11  techcrunch.com       facebook.com            6          1