您编写的代码是计算每个[“小时”、“足够”]组相对于整个DataFrame中“疲劳”列总数的百分比。然而,您想要实现的是获得每个“小时”的“是”和“否”值的“足够”百分比。要做到这一点,您应该计算每个“小时”的“疲劳”值的总和,然后计算每个[“小时”、“足够”]组相对于该总和的百分比。
以下是调整代码的方法:
# First, group by 'Hours' and 'Enough', and count the occurrences.
grouped_df = df.groupby(['Hours', 'Enough']).size().reset_index(name='count')
# Now, group by 'Hours' and sum the counts to get the total count for each 'Hour'.
hours_sum = grouped_df.groupby('Hours')['count'].sum().reset_index(name='hours_sum')
# Merge the two DataFrames on the 'Hours' column.
merged_df = pd.merge(grouped_df, hours_sum, on='Hours')
# Now, calculate the percentage.
merged_df['Tired_percentage'] = (merged_df['count'] / merged_df['hours_sum']) * 100
# If you want, you can sort the DataFrame.
sorted_df = merged_df.sort_values(['Hours', 'Tired_percentage'], ascending=[False, False])
# Display the DataFrame.
sorted_df