简体   繁体   中英

Please make me understand the reason behind why numpy.isfinite function is used in the below code?

** I have a CSV file, containing Oympics data for summers as well as winters for all the countries, the problem statement is -> Which country has the biggest difference between their summer gold medal counts and winter gold medal counts relative to their total gold medal count?

Only include countries that have won at least 1 gold in both summer and winter. **

import numpy as np
    def answer_three():
        copy_df = df.copy()
        copy_df = copy_df.where(df['Gold'] > 0)
        copy_df = copy_df.where(df['Gold.1'] > 0)
        copy_df['Diff'] = copy_df['Gold'] - copy_df['Gold.1']
        copy_df['Gold_Ratio'] = copy_df['Diff'] / (copy_df['Gold'] + copy_df['Gold.1'])
        copy_df_final = copy_df[np.isfinite(copy_df['Gold_Ratio'])]
        max_ratio = max(copy_df_final['Gold_Ratio'])
    
        return (str(copy_df_final[copy_df_final['Gold_Ratio'] == max_ratio].index[0]))
    answer_three()

I Think i might got the solution in the third and fourth line of code copy_df = copy_df.where(df['Gold'] > 0) and copy_df = copy_df.where(df['Gold.1'] > 0) i am only allowing values greater than '0' so, apparently other rows for each columns where values are not >0 are filled with NaN values, and hence the use of isfinite. Please correct me, if my interpretation is wrong.

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