键入错误:使用pandas.apply定位参数

时间:2017-01-12 18:19:46

标签: python pandas numpy

问题陈述:

需要根据两个现有列same_grouprow的值,从布尔值中创建一个pandas数据帧列系列col。如果两个值在字典memberships中具有相似的值(相交值),则行需要显示True,否则为False(没有相交的值)。使用pd.apply()会出错:

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

设定:

import pandas as pd
import numpy as np 
n = np.nan
memberships = {'a':['vowel'], 'b':['consonant'], 'c':['consonant'], 'd':['consonant'], 'e':['vowel'], 'y':['consonant', 'vowel']}

congruent = pd.DataFrame.from_dict(  
         {'row': ['a','b','c','d','e','y'],
            'a': [ n, -.8,-.6,-.3, .8, .01],
            'b': [-.8,  n, .5, .7,-.9, .01],
            'c': [-.6, .5,  n, .3, .1, .01],
            'd': [-.3, .7, .3,  n, .2, .01],
            'e': [ .8,-.9, .1, .2,  n, .01],
            'y': [ .01, .01, .01, .01,  .01, n],
       }).set_index('row')
congruent.columns.names = ['col']

snippet of dataframe cs

cs = congruent.stack().to_frame()
cs.columns = ['score']
cs.reset_index(inplace=True)
cs.head(6)

snippet of dataframe cs stacked

期望的目标:

finest drawing of added pandas column

制作布尔系列:

尝试0:

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']))

看起来我正在向checkGrouping提供args,所以为什么我会收到此错误以及如何解决?

2 个答案:

答案 0 :(得分:1)

apply会沿着它正在迭代的列或行传递给你。因此,您的函数$(logContent)将接收该参数。所以它的正确原型将是:

checkGrouping

答案 1 :(得分:1)

# 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()

enter image description here