如何从分层索引的组对象生成绘图,以便获得两条曲线?

2024-05-19 02:51:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我将数据转换如下:

s1 = pd.DataFrame({'soa': [500, -500, -500, -500, 500, 500],
                   'congruent': [True, True, True, False, False, False],
                   'is_correct': [True, True, False, False, False, False],
                   'some_stuff': pd.np.random.rand(6)})
subdf = s1[['congruent', 'soa', 'is_correct']]  # s1 is a DataFrame
m = subdf.groupby(('soa', 'congruent')).mean()

在ipython笔记本中显示m给了我一张非常漂亮的桌子:

enter image description here

我现在要画两条独立的曲线,每条曲线有两个点(-500和500)。一条曲线是congruent == True的条件,另一条曲线是congruent == False的条件

我试着做m.plot(),但是我得到了一条曲线,每个多索引元组有一个点:

enter image description here

了解熊猫,我想有一个简单的方法来解决这个问题——有什么想法吗

提前谢谢


Tags: 数据falsetruedataframeissome条件曲线
1条回答
网友
1楼 · 发布于 2024-05-19 02:51:36

好吧,经过一番周折之后,我有了一个解决方案,它能满足我的需要,尽管我不太明白它是如何工作的

s1 = pd.DataFrame({'soa': [500, -500, -500, -500, 500, 500],
               'congruent': [True, True, True, False, False, False],
               'is_correct': [True, True, False, False, False, False],
               'some_stuff': pd.np.random.rand(6)})
subdf = s1[['congruent', 'soa', 'is_correct']]  # s1 is a DataFrame
m = subdf.groupby(('soa', 'congruent')).mean()

m.unstack(level=1)
m.unstack(level=1).plot()

结果如下:

enter image description here

Pivot操作仍然把我搞糊涂了,所以如果有人能告诉我为什么level=1是正确的论点,我会非常感激。据我所知,level的意思类似于将这个层次索引级别设置为表标题

相关问题 更多 >

    热门问题