0

I want to get the index and value (non-zero) which have a max total.

A = np.matrix([[0,2,1,0],
               [0,0,0,0],
               [1,3,0,1],])
slst = ['S1','S2','S3','S4']
namelist= ['Alice','John','Joe']
df =  pd.DataFrame(A,columns = slst, index = namelist)
df.loc['total'] = df.select_dtypes(pd.np.number).sum()
print (df)
Output:
       S1  S2  S3  S4
Alice   0   2   1   0
John    0   0   0   0
Joe     1   3   0   1
total   1   5   1   1

And I want to get the result from this table below. How to get it?

Output1:
Alice: 2,
Joe: 3
aloha
  • 23
  • 1
  • 5

2 Answers2

2

This should work also:

df1 = df.max(axis=1)
df1

       S1  S2  S3  S4
Alice   0   2   1   0
John    0   0   0   0
Joe     1   3   0   1
total   1   5   1   1

Alice    2
John     0
Joe      3
total    5
dtype: int64
prosti
  • 42,291
  • 14
  • 186
  • 151
1

Don't do df.loc['total'], try:

total = df.select_dtypes(pd.np.number).sum()
df[total.idxmax()]

Output:

Alice    2
John     0
Joe      3
total    5
Name: S2, dtype: int64
Dan
  • 45,079
  • 17
  • 88
  • 157
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74