Home > Enterprise >  How to make a dictionary from two columns based on condition?
How to make a dictionary from two columns based on condition?

Time:03-13

I have this dataframe :

df

I wanted to map Class wise Cluster based on F_measure . Like For Cluster 0 max F-measure value is 0.055. So Cluster 0 is mapped to Class 26. Can I extract a dictionary from here. Or how can fetch this information. I had tried and unfortunately ended up like this.

e_df['Clus_to_Class'] = e_df.groupby('Cluster')['F_measure'].transform('max')

CodePudding user response:

Group by Cluster, and then transform('idxmax') on F_measure to get the index of the largest F_measure value for each group. Then use df.loc to index the Class column by the result:

df['Clus_to_Class'] = df.loc[df.groupby('Cluster')['F_measure'].transform('idxmax'), 'Class']
  • Related