Python 类型错误:带pandas.apply的位置参数 问题陈述:

Python 类型错误:带pandas.apply的位置参数 问题陈述:,python,pandas,numpy,Python,Pandas,Numpy,需要根据两个现有列的值,row和col从布尔值创建一个数据帧列系列,same_group。如果两个值在字典成员身份中具有相似的值(相交值),则该行需要显示True,否则显示False(无相交值)。使用pd.apply()给出错误: TypeError: ('checkGrouping() takes 2 positional arguments but 3 were given', 'occurred at index row') 设置: 预期目标: 生成布尔级数: 尝试0: 看起来我正

需要根据两个现有列的值,
row
col
从布尔值创建一个数据帧列系列,
same_group
。如果两个值在字典
成员身份中具有相似的值(相交值),则该行需要显示True,否则显示False(无相交值)。使用
pd.apply()
给出错误:

TypeError: ('checkGrouping() takes 2 positional arguments but 3 were given', 'occurred at index row')
设置:

预期目标:

生成布尔级数: 尝试0:
看起来我正在为
检查分组
提供参数,那么为什么会出现此错误以及如何修复此错误?

应用程序将沿着它所迭代的列或行传递给您。因此,函数
checkGrouping
将接收该参数。所以正确的原型应该是:

def checkGrouping(s, row, col):
    if row in memberships.keys() and col in memberships.keys():
        return memberships[row].intersection(set(memberships[col]))
    else:
        return np.nan




产生想要的结果

congruent.stack(dropna=False).reset_index(name='Score') \
    .assign(same_group=np.array(same).astype(int)).dropna()

PS:我认为您试图完成的事情应该用另一种方法来完成,而不是程序性应用,但这是另一种情况,在这里,我只是回答您关于错误参数数量的问题。这个问题实际上是关于正确使用应用,而我不是。我得到了一个新错误:
TypeError:(“'Series'对象是可变的,因此不能对它们进行哈希运算”,u'出现在索引0上”)
。那么,我应该如何对序列值执行查找呢?我应该不使用apply吗?您传递的行和酷参数是系列。就其本身而言,它们并不像我认为的那样是单元格/标量。您的新错误来自这样一个事实,即现在您在m.keys()中执行序列。同样,代码没有反映您的意图。您不应该使用apply,而是直接对DataFrame执行向量操作。如果您有任何想法,也许您可以回答以下问题:
def checkGrouping(row, col):
    if row in memberships.keys() and col in memberships.keys():
        return memberships[row].intersection(set(memberships[col]))
    else:
        return np.nan


cs['same_group'] = cs.apply(checkGrouping,args=(cs['row'], cs['col']))
def checkGrouping(s, row, col):
    if row in memberships.keys() and col in memberships.keys():
        return memberships[row].intersection(set(memberships[col]))
    else:
        return np.nan
# create a series to make it convenient to map
# make each member a set so I can intersect later
lkp = pd.Series(memberships).apply(set)

# get number of rows and columns
# map the sets to column and row indices
n, m = congruent.shape
c = congruent.columns.to_series().map(lkp).values
r = congruent.index.to_series().map(lkp).values
print(c)
[{'vowel'} {'consonant'} {'consonant'} {'consonant'} {'vowel'}
 {'consonant', 'vowel'}]
print(r)
[{'vowel'} {'consonant'} {'consonant'} {'consonant'} {'vowel'}
 {'consonant', 'vowel'}]
# use np.repeat, np.tile, zip to create cartesian product
# this should match index after stacking
# apply set intersection for each pair
# empty sets are False, otherwise True
same = [
    bool(set.intersection(*tup))
    for tup in zip(np.repeat(r, m), np.tile(c, n))
]

# use dropna=False to ensure we maintain the
# cartesian product I was expecting
# then slice with boolean list I created
# and dropna
congruent.stack(dropna=False)[same].dropna()

row  col
a    e      0.80
     y      0.01
b    c      0.50
     d      0.70
     y      0.01
c    b      0.50
     d      0.30
     y      0.01
d    b      0.70
     c      0.30
     y      0.01
e    a      0.80
     y      0.01
y    a      0.01
     b      0.01
     c      0.01
     d      0.01
     e      0.01
dtype: float64
congruent.stack(dropna=False).reset_index(name='Score') \
    .assign(same_group=np.array(same).astype(int)).dropna()