«AttributeError: объект« список »не имеет атрибута« unstack »» при создании подзаговоров в пандах - PullRequest
0 голосов
/ 11 октября 2019

Я создал график из df со следующим кодом:

pri_count = df_2011.groupby(['Priority', df_2011['Create Time'].dt.to_period('m')]).Priority.count()
plot_df1 = pri_count.unstack('Priority').loc[:, 1]
plot_df2 = pri_count.unstack('Priority').loc[:, 2]
plot_df1.index = pd.PeriodIndex(plot_df1.index.tolist(), freq='m')
plot_df2.index = pd.PeriodIndex(plot_df2.index.tolist(), freq='m')
plot_df1.plot(legend=True, title='Graph 1')
plot_df2.plot(legend=True)

(я не уверен, как добавить изображение, но этот код, кажется, производит первый график, который мне нужен.)Кажется, это работает нормально, но проблема в том, что у меня есть 6 df, которые я хочу создать для создания подзаговора 2x3 с заголовками. Я получаю сообщение об ошибке AttributeError: 'list' object has no attribute 'unstack'.

Я попробовал несколько вещей, которые, казалось, пошли правильным путем (у меня было несколько вещей неправильно, прежде чем я исправил), но теперь я застрял в поиске решенияэта ошибка.

Мой код, который не работает для подзаговоров:

pri_count_list = [df_2011.groupby(['Priority', df_2011['Create Time'].dt.to_period('m')]).Priority.count(),
                  df_2012.groupby(['Priority', df_2012['Create Time'].dt.to_period('m')]).Priority.count(),
                  df_2013.groupby(['Priority', df_2013['Create Time'].dt.to_period('m')]).Priority.count(),
                  df_2014.groupby(['Priority', df_2014['Create Time'].dt.to_period('m')]).Priority.count(),
                  df_2015.groupby(['Priority', df_2015['Create Time'].dt.to_period('m')]).Priority.count(),
                  df_2016.groupby(['Priority', df_2016['Create Time'].dt.to_period('m')]).Priority.count()]

fig, ax = plt.subplots(2, 3)
for d, ax in zip(pri_count_list, axes.ravel()):
    for i in pri_count_list:
        plot_df1 = pri_count_list.unstack('Priority').loc[:, 1]
        plot_df2 = pri_count_list.unstack('Priority').loc[:, 2]
        plot_df1.index = pd.PeriodIndex(plot_df1.index.tolist(), freq='m')
        plot_df2.index = pd.PeriodIndex(plot_df2.index.tolist(), freq='m')
        plot_df1.plot(ax=ax, legend=True, figsize=(15, 7))
        plot_df2.plot(ax=ax, legend=True, figsize=(15, 7))

Я хочу создать подзаговор 2x3 тех же типов, что и отдельный граф, который генерирует приведенный выше код.

Я бы хотел больше понять смысл этой ошибки, поэтому объясните, где это возможно. Спасибо

РЕДАКТИРОВАТЬ: Данные от pri_count_list является:

[Priority  Create Time
 0.0       2011-04            1
           2011-09            1
 1.0       2011-01         2801
           2011-02         2358
           2011-03         2688
           2011-04         2958
           2011-05         3162
           2011-06         3260
           2011-07         3605
           2011-08         3361
           2011-09         3300
           2011-10         3135
           2011-11         3028
           2011-12         3043
 2.0       2011-01        11905
           2011-02        10705
           2011-03        12256
           2011-04        12076
           2011-05        11912
           2011-06        12492
           2011-07        12768
           2011-08        12558
           2011-09        11978
           2011-10        11994
           2011-11        11113
           2011-12        11557
 Name: Priority, dtype: int64, Priority  Create Time
 1.0       2012-01         3198
           2012-02         2991
           2012-03         3164
           2012-04         3291
           2012-05         3420
           2012-06         3692
           2012-07         3780
           2012-08         3814
           2012-09         3570
           2012-10         3694
           2012-11         3572
           2012-12         3740
 2.0       2012-01        11562
           2012-02        10756
           2012-03        12441
           2012-04        12027
           2012-05        12104
           2012-06        12809
           2012-07        12935
           2012-08        12683
           2012-09        11872
           2012-10        12484
           2012-11        11638
           2012-12        12193
 Name: Priority, dtype: int64, Priority  Create Time
 0.0       2013-01            4
           2013-03            4
           2013-05            2
           2013-06            1
           2013-07            3
           2013-08            1
           2013-09            2
           2013-10            2
           2013-12            2
 1.0       2013-01         3409
           2013-02         3145
           2013-03         3448
           2013-04         3539
           2013-05         3818
           2013-06         3977
           2013-07         3802
           2013-08         3806
           2013-09         3851
           2013-10         3619
           2013-11         3439
           2013-12         3318
 2.0       2013-01        11792
           2013-02        10718
           2013-03        12010
           2013-04        12212
           2013-05        12344
           2013-06        12626
           2013-07        12679
           2013-08        12894
           2013-09        12617
           2013-10        12229
           2013-11        11449
           2013-12        11289
 Name: Priority, dtype: int64, Priority  Create Time
 1         2014-01         3548
           2014-02         3216
           2014-03         3626
           2014-04         3372
           2014-05         3728
           2014-06         3734
           2014-07         3649
           2014-08         3507
           2014-09         3613
           2014-10         3690
           2014-11         3361
           2014-12         3729
 2         2014-01        11956
           2014-02        11006
           2014-03        12373
           2014-04        11888
           2014-05        12601
           2014-06        12293
           2014-07        12384
           2014-08        11752
           2014-09        11627
           2014-10        12626
           2014-11        11478
           2014-12        12723
 Name: Priority, dtype: int64, Priority  Create Time
 0         2015-10            1
 1         2015-01         3692
           2015-02         3282
           2015-03         3481
           2015-04         3360
           2015-05         3521
           2015-06         3688
           2015-07         3796
           2015-08         3895
           2015-09         3895
           2015-10         3729
           2015-11         2981
           2015-12         3098
 2         2015-01        12211
           2015-02        11669
           2015-03        12657
           2015-04        12303
           2015-05        12858
           2015-06        12761
           2015-07        13505
           2015-08        13117
           2015-09        12799
           2015-10        12610
           2015-11        11621
           2015-12        12051
 Name: Priority, dtype: int64, Priority  Create Time
 1.0       2016-01         3134
           2016-02         3290
           2016-03         3410
           2016-04         3462
           2016-05         3762
           2016-06         3707
           2016-07         3790
 2.0       2016-01        11755
           2016-02        11643
           2016-03        12569
           2016-04        12428
           2016-05        12989
           2016-06        12661
           2016-07        12227
 Name: Priority, dtype: int64]

Ответы [ 2 ]

1 голос
/ 11 октября 2019
# Create subplots
fig, axes = plt.subplots(2, 3)

# Needs to be d, not pri_count_list 
for d, ax in zip(pri_count_list, axes.ravel()):
    plot_df1 = i.unstack('Priority').loc[:, 1]

    # Plot with the iterated axis
    ax.plot(plot_df1, legend=True, figsize=(15,7)
    ...
1 голос
/ 11 октября 2019
for d, ax in zip(pri_count_list, axes.ravel()):
    for i in pri_count_list:   # this i
        plot_df1 = pri_count_list.unstack('Priority').loc[:, 1]  # this should be i

Это должно быть i, так как оно должно быть результатом итерации, а не самого списка.

Так фиксировано:

        plot_df1 = i.unstack('Priority').loc[:, 1]
        plot_df2 = i.unstack('Priority').loc[:, 2]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...