Python Pandas:如何基于条件库中的另一个数组替换数据框中的值 [英] Python Pandas: How to replace values in a Dataframe based on another array in conditional base
问题描述
我有一个DataFrame如下.两列均具有Member_ID,该成员ID指示哪个Member_ID与其他Member_ID连接
I have a DataFrame as follows. Both columns have Member_ID which indicates which Member_ID connected with other Member_ID
col1 col2
1 3
1 4
1 5
2 3
2 4
3 1
3 2
3 5
4 1
4 2
5 1
5 3
并且我已经计算出每个Member_ID与多少个Member_ID相关联.例如,Member_ID 1与3 Member_ID连接.如果Member_ID包含的连接数大于或等于3,则必须在Member_Id前面放置"a",否则必须放置"b",因此对于Member_ID 1,我们必须将标签命名为"a1".
同样,我已经为每个Member_Id计算了标签,标签数组如下.
and I have calculated each Member_ID connected with how many Member_ID. For example Member_ID 1 is connected with 3 Member_ID. If an Member_ID contains more or equal to 3 connections we have to put "a" in front of the Member_Id else we have to put "b" so the label we have to give the label as "a1" for Member_ID 1.
Likewise I have calculated the labels for each Member_Id and the label array is below.
member_ID No_of_con Label
1 3 a1
2 2 b2
3 3 a3
4 2 b4
5 2 b5
现在,我必须替换从标签数组引用的第一个数据框的值.数据框对于使用for循环来说效率不高,那么如何以更简单的方式使用Pandas实现呢?我期望结果如下
Now I have to replace the first Dataframe's values referring from the label array. Dataframe is big for using for loops is not efficient So how can i achive this using Pandas in simpler way? I'm expecting the result as below
col1 col2
a1 a3
a1 b4
a1 b5
b2 a3
b2 b4
a3 a1
a3 b2
a3 b5
b4 a1
b4 b2
b5 a1
b5 a3
推荐答案
我们可以stack
,map
和unstack
:
In [9]: d1.stack().map(d2.set_index('member_ID')['Label']).unstack()
Out[9]:
col1 col2
0 a1 a3
1 a1 b4
2 a1 b5
3 b2 a3
4 b2 b4
5 a3 a1
6 a3 b2
7 a3 b5
8 b4 a1
9 b4 b2
10 b5 a1
11 b5 a3
这篇关于Python Pandas:如何基于条件库中的另一个数组替换数据框中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!