我有数据帧作为,
我需要像这样的东西为每个列,如压力,抑郁和焦虑和每个参与者在每个类别的数据

我把python代码写成
ax = data_full.plot(x="participants", y=["Stress","Depression","Anxiety"],kind="line", lw=3, ls='--', figsize = (12,6))
plt.grid(True)
plt.show()像这样获得输出

发布于 2021-07-27 22:20:09
拆分participant列并将其与原始数据框合并。将数据框更改为仅包含合并数据框中所需列的数据框。通过旋转将数据帧转换为其最终形式。然后,将生成的数据帧用作图形的基础。现在,我们可以调整x轴刻度线、图例位置和y轴限制。
dfs = pd.concat([df,df['participants'].str.split('_', expand=True)],axis=1)
dfs.columns = ['Stress', 'Depression', 'Anxiety', 'participants', 'category', 'group']
fin_df = dfs[['category','group','Stress']]
fin_df = dfs.pivot(index='category', columns='group', values='Stress')
# update
fin_df = fin_df.sort_index(ascending=False)
g = fin_df.plot(kind='line', title='Stress')
g.set_xticks([0,1])
g.set_xticklabels(['pre','post'])
g.legend(loc='center right')
g.set_ylim(5,25)

https://stackoverflow.com/questions/68544417
复制相似问题