pandas datafram中值之间的交集

2024-06-06 08:29:07 发布

您现在位置:Python中文网/ 问答频道 /正文

问题陈述:

创建新的pandas dataframe列,在两个不同的列中显示行值的布尔值为1(交集)或0(无交集):row_modscol_mods。添加另一列以显示这些重叠是什么。在下面的例子中,intersect取布尔值,common显示相交值。在

呈现的pandas数据帧是我所拥有的,绘制部分是我要查找的:

enter image description here

设置:

# data
n = np.nan
congruent = pd.DataFrame.from_dict(  
         {'row': ['x','a','b','c','d','e','y'],
            'x': [ n,  5,   5,  5,  5,  5, 5],
            'a': [ 5, n, -.8,-.6,-.3, .8, .01],
            'b': [ 5,-.8,  n, .5, .7,-.9, .01],
            'c': [ 5,-.6, .5,  n, .3, .1, .01],
            'd': [ 5,-.3, .7, .3,  n, .2, .01],
            'e': [ 5, .8,-.9, .1, .2,  n, .01],
            'y': [ 5, .01, .01, .01, .01,  .01, n],
       }).set_index('row')
congruent.columns.names = ['col']
memberships = {'a':['vowel'], 'b':['consonant'], 'c':['consonant'], 'd':['consonant'], 'e':['vowel'], 'y':['consonant', 'vowel'], '*':['wildcard']}

# format stacked df
cs = congruent.stack().to_frame()
cs.columns = ['score']
cs.reset_index(inplace=True)
cs.columns = ['row', 'col', 'score']

# filter col entries not found in membership dict keys
cs['elim'] = (cs['row'].isin(memberships.keys())) & (cs['col'].isin(memberships.keys()))
cs_2 = cs[cs['elim'] == True]

# map col entires to membership dict values
cs_2['row_mods'] = cs_2['row'].map(memberships)
cs_2['col_mods'] = cs_2['col'].map(memberships)

如何在一行的两个不同列上执行两个值的交集?在


Tags: columnstomodsmappandasindexcolkeys
2条回答

由于您显然对PANDAS操作很熟悉,所以我只提供Python交集逻辑:

common = list(set(row_mods).intersection(set(col_mods)))
intersect = len(common) > 0

简单地说,将每个mod列表转换为一个集合,然后使用Python内置的交集方法。将结果返回到列表中。在

这能解决你的问题吗?在

试试这个伴侣:

第1步,定义函数:

def check_row (row_mods, col_mods):

    common = []

    intersect = 0

    for x in col_mods:

        if x in row_mods:

            intersect = 1
            common.append(x)

    if (intersect == 0):

        common.append(np.nan)

    return (intersect, common)

第2步,应用功能:

^{pr2}$

希望有帮助!如果它支持投票/检查答案:)

相关问题 更多 >