简体   繁体   中英

Returning max value in one column based on changing condition in another column

I have a dataset that looks like this:

|        | season   |  stormid |  vmax |
 
|0       |  1970    | SH071970 |   1 |
|1       |  1970    | SH071971 |   6 |
|2       |  1970    | SH071970 |   3 |
|3       |  1970    | SH071971 |   9 |

And I want to return the max value of vmax for each unique stormid that can be added as an additional column to another dataframe.

So far I have tried

df['vmax'] = df['stormid']['vmax'].max()

With no luck.

Is it maybe possible to write a for loop to loop through the stormid's (which I have stored in another dataframe) and return the max value from the vmax column?

Use groupby . Example code is here.

df.groupby('stromid')['vmax'].max()

It returns pandas Series whose index is stormid and value is the max of vmax according to the stromid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM