# Maximum of Series' means with panda

65
April 18, 2018, at 07:56 AM

I've been searching for a while now and I can't figure out how to do that in other way than hardcoding it. In a csv file, I have to obtain the maximum value of the mean of three series, and then return the NAME of that series, which is the part causing me troubles.

``````comp = max(DataTD['Cycle (seconds)'].mean(), DataTD['Run (seconds)'].mean(), DataTD['Swim (seconds)'].mean())
if comp == DataTD['Cycle (seconds)'].mean():
print(DataTD['Cycle (seconds)'].name)
elif comp == DataTD['Run (seconds)'].mean():
print(DataTD['Run (seconds)'].name)
elif comp == DataTD['Swim (seconds)'].mean():
print(DataTD['Swim (seconds)'].name)
``````

Something like this should work (haven't test it):

``````datas = [DataTD['Cycle (seconds)'],
DataTD['Run (seconds)'],
DataTD['Swim (seconds)']]
means = [data.mean() for data in datas]
max_mean_idx = np.argmax(means)
print(datas[max_mean_idx].name)
``````

You can get the maximum column name, then based on that grab the mean.

``````cols = ['Cycle (seconds)', 'Run (seconds)', 'Swim (seconds)']
max_col = max(cols, key=lambda col: DataTD[col].mean())
print('Column name: ' + max_col)
print('Mean: ' + str(DataTD[max_col].mean()))
``````

Data From Pir

``````DataTD.loc[DataTD[cols].mean(1).sort_values().iloc[[-1]].index]
Out:
Cycle (seconds)  Run (seconds)  Swim (seconds)
J                7              5               9
``````

Consider the sample data

``````cols = ['Cycle (seconds)', 'Run (seconds)', 'Swim (seconds)']
np.random.seed([3, 1415])
DataTD = pd.DataFrame(
np.random.randint(10, size=(10, 3)),
list('ABCDEFGHIJ'), cols
)
Cycle (seconds)  Run (seconds)  Swim (seconds)
A                0              2               7
B                3              8               7
C                0              6               8
D                6              0               2
E                0              4               9
F                7              3               2
G                4              3               3
H                6              7               7
I                4              5               3
J                7              5               9
``````

IIUC:

Use `mean` with argument `axis=1` followed with an `idxmax` to find the index for the maximal value.

``````DataTD.loc[[DataTD[cols].mean(1).idxmax()]]
Cycle (seconds)  Run (seconds)  Swim (seconds)
J                7              5               9
``````

Otherwise, try

``````means = DataTD.mean()
means[[means.idxmax()]]
Swim (seconds)    5.7
dtype: float64
``````
POPULAR ONLINE

#### Validating user token from backend ### converting spacy token vectors into text

I am using spacy to create vectors of a sentenceIf the sentence is 'I am working', it gives me a vector of shape (3, 300)

119 ### Flask 405 on POST even when POST is present

This is my code, a very simple programEven though POST is present, server is showing 405 on POST request

91 ### Converting Flask session to Django REST Framework session

Converting Flask session to Django REST Framework session(How do I get a Django REST Framework session that is now in flask

132 ### Clause extration - Spacy

Intention is to extract clauses to pandas dataframe and to excel

74