繁体   English   中英

用来自另一个数据框的匹配ID替换熊猫中的单元格值

[英]Replace Cell Values in Pandas with matching ID from another dataframe

我有一个数据框,其中包含通过熊猫库存储的域(或本例中的顶点/节点)列表:

                 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

因为此数据集将变得更大,所以我读到如果仅使用整数而不是字符串表示“ edges”数据框,则可以提高性能。

因此,我想知道是否存在一种快速的方法来用域(又称为顶点)数据框中的相应ID替换边缘数据框中的每个单元格? 因此,边缘数据框中的第1行可能看起来像这样:

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

我该怎么做呢? 先感谢您。

这是分类数据的好用例: http : //pandas.pydata.org/pandas-docs/stable/categorical.html

简而言之,分类系列将内部将每个项目表示为一个数字,但将其显示为一个字符串。 当您有很多重复的字符串时,这很有用。

与将所有内容手动转换为整数相比,使用“分类系列”更容易且更容易出错。

我尝试实现另一个答案-转换为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

最简单的方法是从顶点数据帧生成一个字典... 如果可以确定它代表了将出现在边缘的确定的顶点集...并将其与replace一起replace

由于顶点数据框的索引已经具有因子信息...

m = dict(zip(vertices.domain, vertices.index))
edges.replace(m)

    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

您也可以使用stack / map / unstack

m = dict(zip(vertices.domain, vertices.index))
edges.stack().map(m).unstack()

    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

社论

除了提供我自己的信息之外,我还想评论@JohnZwinck的答案。

首先, categorical将提供更快的性能。 但是,我尚不清楚一种确保您可以拥有两列协调类别的方法。 我所说的协调是指每列在后台分配给每个类别的一组整数。 我们知道(或不知道)这些整数相同的方法。 如果我们将它做成一个大列,然后将该列转换为分类列,那将起作用...但是,我相信一旦我们再次分成两列,它将变成对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM