将 python 中的枢轴 table 转换为可视化图形

Converting a pivot table in python to a visualized graph

我正在尝试将我的枢轴 table 转换为绘图。这就是我创建枢轴的方式:

df_posts ['Year_created'] = df_posts['CreationDate'].dt.year
df_posts ['Month_created'] = df_posts['CreationDate'].dt.month
pst_pivot = df_posts.pivot_table(index='Year_created', columns='Month_created', values='Title', aggfunc='count')

pst_pivot.head()

正在尝试获取这个或类似的:

谢谢!

  • 定义多索引
  • unstack() 作为列
  • 然后 plot()
d = pd.date_range("1-Oct-2011", "1-sep-2013", freq="M")
df = pd.DataFrame({"CreationDate":d, "value":np.random.randint(70,90, len(d))})

df.set_index(pd.MultiIndex.from_arrays([df.CreationDate.dt.year, 
                                        df.CreationDate.dt.month], 
                                       names=["Year_created","Month_created"])).loc[:,"value"].unstack(0).plot()