Calculating no of non-zero in a column corresponding to another column

时间:2018-10-02 09:08:10

标签: python pandas

I have a dataframe:

 d = {'class': [0, 1,1,0,1,0], 'A': [0,4,8,1,0,0],'B':[4,1,0,0,3,1]}
 df = pd.DataFrame(data=d)

which looks like-

    A   B   class
0   0   4   0
1   4   1   1
2   8   0   1
3   1   0   0
4   0   3   1
5   0   1   0

I want to calculate for each column the corresponding a,b,c,d which are no of non-zero in column corresponding to class column 1,no of non-zero in column corresponding to class column 0,no of zero in column corresponding to class column 1,no of zero in column corresponding to class column 0

for example-

for column A the a,b,c,d are 2,1,1,2

explantion- In column A we see that where column[class]=1 the number of non zero values in column A are 2 therefore a=2(indices 1,2).Similarly b=1(indices 3)

My attempt(when the dataframe had equal no of 0 and 1 class)-

dataset = pd.read_csv('aaf.csv')

n=len(dataset.columns)  #no of columns

X=dataset.iloc[:,1:n].values

l=len(X) #no or rows


score = []

for i in range(n-1):
    #print(i)

    X_column=X[:,i]
    neg_array,pos_array=np.hsplit(X_column,2)##hardcoded 
    #print(pos_array.size)
    a=np.count_nonzero(pos_array)
    b=np.count_nonzero(neg_array)
    c= l/2-a
    d= l/2-b

1 个答案:

答案 0 :(得分:1)

Use:

d = {'class': [0, 1,1,0,1,0], 'A': [0,4,8,1,0,0],'B':[4,1,0,0,3,1]}
df = pd.DataFrame(data=d)

df = (df.set_index('class')
       .ne(0)
       .stack()
       .groupby(level=[0,1])
       .value_counts()
       .unstack(1)
       .sort_index(level=1, ascending=False)
       .T)
print (df)
class     1     0     1     0
      True  True  False False
A         2     1     1     2
B         2     2     1     1

df.columns = list('abcd')
print (df)
   a  b  c  d
A  2  1  1  2
B  2  2  1  1
相关问题