Home > Mobile >  Get max for all columns between row index 0 and row index X
Get max for all columns between row index 0 and row index X

Time:01-28

If I have dataframe with 4 columns and N rows:

data = {'col0' : [85, 75, 85, 87, 91, 83],
        'col1' : [85, 75, 85, 87, 75, 81],
        'col2' : [35, 45, 83, 35, 45, 83],
        'col3' : [51, 61, 45, 51, 91, 45]}
frame = pd.DataFrame(data)
print(frame)

so here's frame:

   col0  col1  col2  col3
0    85    85    35    51
1    75    75    45    61
2    85    85    83    45
3    87    87    35    51
4    91    75    45    91
5    83    81    83    45

what's an elegant way to get maximum for each column between row index 0 and row index X, where X (the row index) is different for each column, and available in an array. So len(arrX) == len(df.columns).

CodePudding user response:

Here is something you could try:

arrX= [2,3,4,5]
max_cols = {'max_' col_name: max(frame[col_name][0:arrX[i]]) for i,col_name in enumerate(frame.columns)}

Answer:

print(max_cols)
>>> {'max_col0': 85, 'max_col1': 85, 'max_col2': 83, 'max_col3': 91}
  •  Tags:  
  • Related