Python 熊猫:如何绘制特定的列和案例?
我有一个如下所示的数据帧Python 熊猫:如何绘制特定的列和案例?,python,pandas,Python,Pandas,我有一个如下所示的数据帧 Zone year Intervention A Intervention B 0 Zone 1 2005 0.896788 0.892161 1 Zone 1 2006 0.807323 0.809103 2 Zone 1 2007 0.758814 0.764219 3 Zone 2 2005 0.697728 0.699586 4 Zo
Zone year Intervention A Intervention B
0 Zone 1 2005 0.896788 0.892161
1 Zone 1 2006 0.807323 0.809103
2 Zone 1 2007 0.758814 0.764219
3 Zone 2 2005 0.697728 0.699586
4 Zone 2 2006 0.649360 0.648350
5 Zone 2 2017 0.566785 0.571259
我想绘制4条不同的曲线,其中x轴上有年份
,y轴上有间隔值
这4条曲线是:
干预A
区域1
干预A
区域2
干预B
区域1
干预B
区域2
第一个想法是使用并展平列中的多索引:
df1 = df.pivot(index='year', columns='Zone')
df1.columns = df1.columns.map('_'.join)
df1.plot()
另一个是分别筛选每个子数据帧:
df11 = df.loc[df['Zone'].eq('Zone 1')]
df22 = df.loc[df['Zone'].eq('Zone 2')]
ax = df11.plot(x='year', y='Intervention A')
df22.plot(x='year', y='Intervention A', ax=ax)
df11.plot(x='year', y='Intervention B', ax=ax)
df22.plot(x='year', y='Intervention B', ax=ax)
第一个想法是使用并展平列中的多索引
:
df1 = df.pivot(index='year', columns='Zone')
df1.columns = df1.columns.map('_'.join)
df1.plot()
另一个是分别筛选每个子数据帧:
df11 = df.loc[df['Zone'].eq('Zone 1')]
df22 = df.loc[df['Zone'].eq('Zone 2')]
ax = df11.plot(x='year', y='Intervention A')
df22.plot(x='year', y='Intervention A', ax=ax)
df11.plot(x='year', y='Intervention B', ax=ax)
df22.plot(x='year', y='Intervention B', ax=ax)