我有一个时间索引表,该表的BLIP列只有两个值“ XX”和“ YY”。目的是显示x轴以下的计数“ XX”和“ YY”,其中“ YY”。我正在尝试使用Wes McKenney关于数据分析的书(我认为是第26页)中的代码从熊猫表中创建正确的数据结构:
df = base_df.drop(columns=dropcols).set_index('Created')
group = ['f2','BLIP']
df0 = df_minus.groupby(group)
agg_counts = df0.size().unstack().fillna(0)
indexer = agg_counts.sum(1).argsort()
count_subset = agg_counts.take(indexer).copy()
table = count_subset.groupby('BLIP').resample('MS').count().unstack('BLIP')['BLIP']
chart = table.plot.bar(title = chart_title, x=None, color = ['green', 'red', 'grey']);
线
agg_counts = df0.size().unstack().fillna(0)
导致以下错误:
TypeError: 'numpy.int32' object is not callable
我在这里找到了一段代码的宝石,但是找不到解密它的文档。
data['values'].plot(kind='bar', color=data.positive.map({True: 'g', False: 'r'}))
这似乎是非常简单的,但是我对此颇为关注。
大熊猫表格式类似于
create_date f1 f2 f3 BLIP f5...
dt_stamp X Y Z XX K1
dt_stamp S R Y YY K3
dt_stamp P P T XX K1
等等。
我尝试过杰西的建议
df_plus =df[df['BLIP']=='XX']
df_minus=df[df['BLIP']=='YY']
ax = plt.axes()
ax.bar(df_plus.index, df_plus['BLIP'], width=0.4, color='g')
ax.bar(df_neg.index, df_minus['BLIP'], width=0.4, color='r')
ax.autoscale()
plt.show()
这导致
ValueError: shape mismatch: objects cannot be broadcast to a single shape
整体解决方案
df = base_df
plt.clf()
fig = plt.figure()
width = 8
height = 6
fig.set_size_inches(width, height)
chart_title = 'YTD CR Trend Summary'
df_plus =df[df['BLIP'] == 'XX']
df_minus=df[df['BLIP']== 'IYY']
p = df_plus.resample('MS').count()['BLIP'].fillna(0)
n = df_minus.resample('MS').count()['BLIP'].apply(lambda x: int(-x))
print(chart_title, len(df), p.sum(), n.sum())
plt.clf()
fig = plt.figure()
# ax = fig.add_subplot(1, 1, 1)
ax = plt.axes(label=chart_title) #label suppresses warning
if p.sum() != False:
ax.bar(p.index, p, width=10, color='g')
if n.sum() != False:
ax.bar(n.index, n, width=10, color='r')
plt.suptitle(chart_title, fontsize=11)
filename = f'{graph_images_dir}{chart_title}.png'
print(f'Saving {filename}')
plt.savefig(filename, bbox_inches='tight', pad_inches=0.5, dpi=200)
plt.show()
相关分类