Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过绘制按顶层分组的多索引来分组_Python_Pandas_Plot_Pandas Groupby_Multi Index - Fatal编程技术网

Python 通过绘制按顶层分组的多索引来分组

Python 通过绘制按顶层分组的多索引来分组,python,pandas,plot,pandas-groupby,multi-index,Python,Pandas,Plot,Pandas Groupby,Multi Index,我正在努力按照我想要的方式制作熊猫groupby多索引图。我有以下虚拟数据帧: data = { 'Day': [1, 1, 2, 2, 3, 3, 4, 2, 4], 'Condition': ['A', 'B', 'A', 'A', 'A', 'B', 'B', 'B', 'A'], 'Invest': [1100, 2002, 500, 200, 1030, 4000, 750, 5000, 320], 'Spent': [100, 200, 100, 10

我正在努力按照我想要的方式制作熊猫groupby多索引图。我有以下虚拟数据帧:

data = {
    'Day': [1, 1, 2, 2, 3, 3, 4, 2, 4],
    'Condition': ['A', 'B', 'A', 'A', 'A', 'B', 'B', 'B', 'A'],
    'Invest': [1100, 2002, 500, 200, 1030, 4000, 750, 5000, 320],
    'Spent': [100, 200, 100, 100, 100, 200, 50, 300, 250]
}

index = range(len(data['Day']))

columns = ['Day', 'Condition', 'Invest', 'Spent']

df = pd.DataFrame(data, index=index, columns=columns)

+----+-------+-------------+----------+---------+
|    |   Day | Condition   |   Invest |   Spent |
|----+-------+-------------+----------+---------|
|  0 |     1 | A           |     1100 |     100 |
|  1 |     1 | B           |     2002 |     200 |
|  2 |     2 | A           |      500 |     100 |
|  3 |     2 | A           |      200 |     100 |
|  4 |     3 | A           |     1030 |     100 |
|  5 |     3 | B           |     4000 |     200 |
|  6 |     4 | B           |      750 |      50 |
|  7 |     2 | B           |     5000 |     300 |
|  8 |     4 | A           |      320 |     250 |
+----+-------+-------------+----------+---------+
我可以使用以下方法获得后续绘图:

df.groupby(['Day', 'Condition']).sum()\
   .unstack()\
   .plot(subplots=True, 
    layout=(2,2),
    figsize=(8,6));

问题:我希望将A和B结果分组在一起。例如,顶部地块,即(投资,A)和(投资,B)在一个地块内(类似于已用地块)。因此,我只有2个子图,而不是4个子图。我在stackoverflow中有很多例子,但仍然无法使其工作。有人建议融化和使用海洋生物,但仍然不起作用,我更喜欢使用熊猫

附言:我所说的“顶级”是什么意思?我是否在这里使用了正确的术语,还不确定,但当我取消按熊猫分组时,在多索引中有不同的级别,我的意思是根据顶部级别对绘图进行分组,如下所示:

df.groupby(['Day', 'Condition'])\
   .sum()\
   .unstack()

您可以很容易地将其一分为二

import matplotlib as plt
df1 = df.groupby(['Day', 'Condition']).sum().unstack()

print(df1)

          Invest       Spent     
Condition      A     B     A    B
Day                              
1           1100  2002   100  200
2            700  5000   200  300
3           1030  4000   100  200
4            320   750   250   50
过滤df1中的“投资”和绘图。(我不知道如何将jupyter的图表输出复制到这里。对不起。)

现在筛选“已使用的”

df1.loc[:,('Spent', slice(None))].plot(subplots=True, 
    layout=(1,2),
    figsize=(10,4));
我想这样做:

df=df.groupby(['Day', 'Condition']).sum()\
       .unstack()

df["Invest"].plot(figsize=(8,6), title="Invest")
df["Spent"].plot(figsize=(8,6), title="Spent")

plt.show()


谢谢它很有魅力。我希望我能找到另一种方法,而不必明确给出列的名称。到目前为止,你的工作还不错。接受。。。
df=df.groupby(['Day', 'Condition']).sum()\
       .unstack()

df["Invest"].plot(figsize=(8,6), title="Invest")
df["Spent"].plot(figsize=(8,6), title="Spent")

plt.show()