'How to make a dictionary from two columns based on condition?
I have this dataframe :
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')
Solution 1:[1]
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']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | richardec |

