如何绘制只包含真值的变量


fig, ax =plt.subplots(1,5,figsize=(25,5))
sns.countplot(x='Foraging',data=df,palette='Pastel1',ax=ax[0])
sns.countplot(x='Eating',data=df,palette='Pastel1',ax=ax[1])
sns.countplot(x='Climbing',data=df,palette='Pastel1',ax=ax[2])
sns.countplot(x='Chasing',data=df,palette='Pastel1',ax=ax[3])
sns.countplot(x='Running',data=df,palette='Pastel1',ax=ax[4])
fig.show()

如何绘制只包含'true'的变量?

我试过了df['Foraging']=df['Foraging']==1但不工作

您可以使用loc方法选择Foraging列等于true的行,然后绘制结果数据框:

# Select rows where 'Foraging' is 'true'
foraging_df = df.loc[df['Foraging'] == 'true']
# Plot the count of 'Foraging'
sns.countplot(x='Foraging', data=foraging_df, palette='Pastel1')

或者,您可以使用查询方法使用布尔表达式来过滤数据框:

# Select rows where 'Foraging' is 'true'
foraging_df = df.query('Foraging == "true"')
# Plot the count of 'Foraging'
sns.countplot(x='Foraging', data=foraging_df, palette='Pastel1')

最新更新